每一個上下文實例都有一個ChangeTracker,它負責跟蹤須要寫入數據庫的更改。更改實體類的實例時,這些更改會記錄在ChangeTracker中,而後在調用SaveChanges時會被寫入數據庫中。此數據庫提供程序負責將更改轉換爲特定於數據庫的操做(例如,關係數據庫的INSERT、UPDATE和DELETE命令)。數據庫
瞭解如何使用上下文和實體類添加、修改和刪除數據。post
使用DbSet.Add方法添加實體類的新實例。調用SaveChanges時,數據將插入到數據庫中。spa
using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; context.Blogs.Add(blog); context.SaveChanges(); }
EF將自動檢測對由上下文跟蹤的現有實體所作的更改。這包括從數據庫加載查詢的實體,以及以前添加並保存到數據庫的實體。只需經過賦值來修改屬性,而後調用SaveChanges便可。code
using (var context = new BloggingContext()) { var blog = context.Blogs.First(); blog.Url = "http://sample.com/blog"; context.SaveChanges(); }
使用DbSet.Remove方法刪除實體類的實例。若是實體已存在於數據庫中,則將在SaveChanges期間刪除該實體。若是實體還沒有保存到數據庫(即跟蹤爲「已添加」),則在調用SaveChanges時,該實體會從上下文中移除且再也不插入。blog
using (var context = new BloggingContext()) { var blog = context.Blogs.First(); context.Blogs.Remove(blog); context.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(); }
除了獨立實體之外,還能夠使用模型中定義的關係。rem
若是建立多個新的相關實體,則將其中一個添加到上下文時也會添加其餘實體。在下面的示例中,博客和三個相關文章會所有插入到數據庫中。找到並添加這些文章,由於它們能夠經過Blog.Posts導航屬性訪問。get
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(); }
若是從已由上下文跟蹤的實體的導航屬性中引用新實體,則將發現該實體並將其插入到數據庫中。在下面的示例中,插入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(); }
若是更改實體的導航屬性,則將對數據庫中的外鍵列進行相應的更改。在下面的示例中,post實體更新爲屬於新的blog實體,由於其Blog導航屬性設置爲指向blog,blog也會插入到數據庫中,由於它是已由上下文post跟蹤的實體的導航屬性引用的新實體。it
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(); }
刪除行爲在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.
事務容許以原子方式處理多個數據庫操做。若是已提交事務,則全部操做都會成功應用到數據庫。若是已回滾事務,則全部操做都不會應用到數據庫。
能夠使用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 } } }
因爲工做繁忙緣由,EF系列在這裏也就完結了,暫時沒有太多時間記錄下去了。今天這個章節也偷了個懶,稍微精簡一點,具體官方說明,我會在下面貼上的,請見諒。
參考文獻:
基本保存
保存相關數據
級聯刪除
使用事務