Entity Framework入門教程(9)---離線場景附加實體圖集到上下文


附加離線實體圖集到上下文

這節主要內容是通過不同的方法將離線實體附加到上下文中。

在離線場景中,保存一個實體要略微困難一些。當我們保存一個離線的實體圖集或一個單獨的離線實體時,我們需要做兩件事。首先,我們要把實體附加到新的上下文中,讓上下文了知道存在這些實體。其次,我們需要手動設置每個實體的EntityState,因為新的上下文不知道這些離線實體都經過了些什么操作,所以新的上下文不能自動地給實體添加EntityState。

下圖說明了此過程。

為了將離線實體附加到上下文,並為實體圖中的每個實體設置EntityState,EF提供下邊幾種方法:

1.DbContext.Entry(entity).State=EntityState.Added/Modified/Deleted

DbContext.Entry()方法返回一個指向特定實體的DbEntityEntry對象,這個DbEntityEntry對象提供有關實體實例的各種信息,我們也可以使用DbEntityEntry對象來操作實體。最重要的是,我們可以通過DbEntityEntry對象的state屬性來指定實體的EntityState如下所示:
 context.Entry(entity).state = EntityState.Added/Modified/Deleted 
一個栗子:

var student = new Student() { //Root entity (無主鍵值)
    StudentName = "Bill",
    StandardId = 1,
    Standard = new Standard() //Child entity (有主鍵值)
        {
        StandardId = 1,
        StandardName = "Grade 1"
      },
 Courses = new List<Course>() {
  new Course(){ CourseName = "Machine Language" }, //Child entity (無主鍵值)
  new Course(){ CourseId = 2 } //Child entity (有主鍵值)
  }
 };

using (var context = new SchoolDBEntities())
{
  context.Entry(student).State = EntityState.Added;
  
    //context.ChangeTracker.Entities返回context追蹤的所有EntityEntry實例
  foreach (var entity in context.ChangeTracker.Entries()){
    Console.WriteLine("{0}: {1}", entity.Entity.GetType().Name, entity.State);
  } 
}

//-----輸出:
Student: Added 
Standard: Added
Course: Added
Course: Added    

在上邊的栗子中,Student實體圖集包含了Standard和Course實體, context.Entry(student).State = EntityState.Added;  將父實體和子實體(無論有沒有主鍵值)的EntityState都設置成Added,所以我們要謹慎使用Entry()方法。
下表說明Entry()方法的規則

父實體 子實體
Added Added
Modified Unchanged
Deleted All child entities will be null

2.DbSet.Add()

DbSet.Add()方法將整個實體圖集附加到上下文中,同時把父實體和子實體的狀態都設置成Added
一個栗子:

//離線實體圖集
Student disconnectedStudent = new Student() { StudentName = "New Student" };
disconnectedStudent.StudentAddress = new StudentAddress() { Address1 = "Address", City = "City1" };

using (var context = new SchoolDBEntities())
{
    context.Students.Add(disconnectedStudent);

    // 獲取EntityEntry實例用於查看實體的狀態
    var studentEntry = context.Entry(disconnectedStudent);
    var addressEntry = context.Entry(disconnectedStudent.StudentAddress);

    Console.WriteLine("Student: {0}", studentEntry.State);
    Console.WriteLine("StudentAddress: {0}", addressEntry.State);
}
//輸出
Student: Added 
StudentAddress: Added

Dbset.Add()方法附加整個實體圖集到上下文中,所有實體的狀態都是Added,執行SaveChange()方法時會執行Insert操作,在數據庫添加新記錄

3.DbSet.Attach()

DbSet.Attach()方法將實體圖集附加到一個新的上下文中,每個實體的狀態都是Unchanged.
一個栗子:

//離線實體圖集
Student disconnectedStudent = new Student() { StudentName = "New Student" };
disconnectedStudent.StudentAddress = new StudentAddress() { Address1 = "Address", City = "City1" };

using (var context = new SchoolDBEntities())
{
    context.Students.Attach(disconnectedStudent);

    // 獲取EntityEntry實例用於查看實體的狀態
    var studentEntry = context.Entry(disconnectedStudent);
    var addressEntry = context.Entry(disconnectedStudent.StudentAddress);

    Console.WriteLine("Student: {0}",studentEntry.State);
    Console.WriteLine("StudentAddress: {0}",addressEntry.State);
}
//----輸出
Student: Unchanged 
StudentAddress: Unchanged

 

EF系列目錄鏈接:Entity Franmework系列教程匯總


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM