1.簡介
每個背景關系實體都有一個ChangeTracker,它負責跟蹤需要寫入資料庫的更改,更改物體類的實體時,這些更改會記錄在ChangeTracker中,然后在呼叫SaveChanges時會被寫入資料庫中,此資料庫提供程式負責將更改轉換為特定于資料庫的操作(例如,關系資料庫的INSERT、UPDATE和DELETE命令),
2.基本保存
了解如何使用背景關系和物體類添加、修改和洗掉資料,
2.1添加資料
使用DbSet.Add方法添加物體類的新實體,呼叫SaveChanges時,資料將插入到資料庫中,
using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; context.Blogs.Add(blog); context.SaveChanges(); }
2.2更新資料
EF將自動檢測對由背景關系跟蹤的現有物體所做的更改,這包括從資料庫加載查詢的物體,以及之前添加并保存到資料庫的物體,只需通過賦值來修改屬性,然后呼叫SaveChanges即可,
using (var context = new BloggingContext()) { var blog = context.Blogs.First(); blog.Url = "http://sample.com/blog"; context.SaveChanges(); }
2.3洗掉資料
使用DbSet.Remove方法洗掉物體類的實體,如果物體已存在于資料庫中,則將在SaveChanges期間洗掉該物體,如果物體尚未保存到資料庫(即跟蹤為“已添加”),則在呼叫SaveChanges時,該物體會從背景關系中移除且不再插入,
using (var context = new BloggingContext()) { var blog = context.Blogs.First(); context.Blogs.Remove(blog); context.SaveChanges(); }
2.4單個SaveChanges中的多個操作
可以將多個添加/更新/洗掉操作合并到對SaveChanges的單個呼叫,
using (var context = new BloggingContext()) { // add context.Blogs.Add(new Blog { Url = "http://sample.com/blog_one" }); context.Blogs.Add(new Blog { Url = "http://sample.com/blog_two" }); // update var firstBlog = context.Blogs.First(); firstBlog.Url = ""; // remove var lastBlog = context.Blogs.Last(); context.Blogs.Remove(lastBlog); context.SaveChanges(); }
3.保存關聯資料
除了獨立物體以外,還可以使用模型中定義的關系,
3.1添加關聯資料
如果創建多個新的相關物體,則將其中一個添加到背景關系時也會添加其他物體,在下面的示例中,博客和三個相關文章會全部插入到資料庫中,找到并添加這些文章,因為它們可以通過Blog.Posts導航屬性訪問,
using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://blogs.msdn.com/dotnet", Posts = new List<Post> { new Post { Title = "Intro to C#" }, new Post { Title = "Intro to VB.NET" }, new Post { Title = "Intro to F#" } } }; context.Blogs.Add(blog); context.SaveChanges(); }
3.2添加相關物體
如果從已由背景關系跟蹤的物體的導航屬性中參考新物體,則將發現該物體并將其插入到資料庫中,在下面的示例中,插入post物體,因為該物體會添加到已從資料庫中提取的blog物體的Posts屬性,
using (var context = new BloggingContext()) { var blog = context.Blogs.Include(b => b.Posts).First(); var post = new Post { Title = "Intro to EF Core" }; blog.Posts.Add(post); context.SaveChanges(); }
3.3更改關系
如果更改物體的導航屬性,則將對資料庫中的外鍵列進行相應的更改,在下面的示例中,post物體更新為屬于新的blog物體,因為其Blog導航屬性設定為指向blog,blog也會插入到資料庫中,因為它是已由背景關系post跟蹤的物體的導航屬性參考的新物體,
using (var context = new BloggingContext()) { //新增一個主體物體 var blog = new Blog { Url = "http://blogs.msdn.com/visualstudio" }; var post = context.Posts.First(); //post更新關系 post.Blog = blog; context.SaveChanges(); }
4.級聯洗掉
洗掉行為在DeleteBehavior列舉器型別中定義,并且可以傳遞到OnDelete Fluent API來控制:
●可以洗掉子項/依賴項
●子項的外鍵值可以設定為null
●子項保持不變
示例:
var blog = context.Blogs.Include(b => b.Posts).First(); var posts = blog.Posts.ToList(); DumpEntities(" After loading entities:", context, blog, posts); context.Remove(blog); DumpEntities($" After deleting blog '{blog.BlogId}':", context, blog, posts); try { Console.WriteLine(); Console.WriteLine(" Saving changes:"); context.SaveChanges(); DumpSql(); DumpEntities(" After SaveChanges:", context, blog, posts); } catch (Exception e) { DumpSql(); Console.WriteLine(); Console.WriteLine($" SaveChanges threw {e.GetType().Name}: {(e is DbUpdateException ? e.InnerException.Message : e.Message)}"); }
記錄結果:
After loading entities: Blog '1' is in state Unchanged with 2 posts referenced. Post '1' is in state Unchanged with FK '1' and reference to blog '1'. Post '2' is in state Unchanged with FK '1' and reference to blog '1'. After deleting blog '1': Blog '1' is in state Deleted with 2 posts referenced. Post '1' is in state Unchanged with FK '1' and reference to blog '1'. Post '2' is in state Unchanged with FK '1' and reference to blog '1'. Saving changes: DELETE FROM [Posts] WHERE [PostId] = 1 DELETE FROM [Posts] WHERE [PostId] = 2 DELETE FROM [Blogs] WHERE [BlogId] = 1 After SaveChanges: Blog '1' is in state Detached with 2 posts referenced. Post '1' is in state Detached with FK '1' and no reference to a blog. Post '2' is in state Detached with FK '1' and no reference to a blog.
5.事務
事務允許以原子方式處理多個資料庫操作,如果已提交事務,則所有操作都會成功應用到資料庫,如果已回滾事務,則所有操作都不會應用到資料庫,
5.1控制事務
可以使用DbContext.Database API開始、提交和回滾事務,以下示例顯示了兩個SaveChanges()操作以及正在單個事務中執行的LINQ查詢,并非所有資料庫提供應用程式都支持事務的, 呼叫事務API時,某些提供應用程式可能會引發例外或不執行任何操作,
using (var context = new BloggingContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" }); context.SaveChanges(); context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/visualstudio" }); context.SaveChanges(); var blogs = context.Blogs .OrderBy(b => b.Url) .ToList(); // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails transaction.Commit(); } catch (Exception) { // TODO: Handle failure } } }
6.總結
由于作業繁忙原因,EF系列在這里也就完結了,暫時沒有太多時間記錄下去了,今天這個章節也偷了個懶,稍微精簡一點,具體官方說明,我會在下面貼上的,請見諒,
參考文獻:
基本保存
保存相關資料
級聯洗掉
使用事務
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/71840.html
標籤:其他
