每一個DBContext實例都有一個ChangeTracker,負責跟蹤須要寫入數據庫的更改。當實例發生更改時,更改會被記錄在ChangeTracker中,在調用 SaveChanges
時被寫入數據庫。sql
使用 DbSet.Add()添加實體類的新實例。 調用 SaveChanges() 時,數據將插入到數據庫中。數據庫
using (var context = new LibingContext()) { var role = new Role { RoleName = "教師" }; context.Roles.Add(role); context.SaveChanges(); }
Entity Framwork Core將自動檢測對由DbContext跟蹤的實體所作的更改。ide
更新數據:修改屬性值,調用 SaveChanges()。spa
using (var context = new LibingContext()) { var role = context.Roles.Find(1); role.RoleName = "教師"; context.SaveChanges(); }
使用 DbSet.Remove() 刪除實體類的實例。code
若是實體已存在於數據庫中,則 SaveChanges() 將刪除該實體。 若是實體還沒有保存到數據庫(即跟蹤爲「已添加」),則 SaveChanges() 時,該實體會從上下文中刪除且再也不插入。blog
using (var context = new LibingContext()) { var role = context.Roles.Find(1); context.Roles.Remove(role); context.SaveChanges(); }
能夠將多個添加/更新/刪除操做合併到對「SaveChanges」的單個調用。事務
對於大多數數據庫提供程序,「SaveChanges」是事務性的。ci
using (var context = new LibingContext()) { // 新增 context.Roles.Add(new Role { RoleName = "管理員" }); context.Roles.Add(new Role { RoleName = "學生" }); // 修改 var modifyRole = context.Roles.Find(1); modifyRole.RoleName = "教師"; // 刪除 var deleteRole = context.Roles.Where(t => t.RoleID == 2).FirstOrDefault(); context.Roles.Remove(deleteRole); context.SaveChanges(); }
using (var context = new LibingContext()) { var category = new Category { CategoryName = "手機", Products = new List<Product> { new Product { ProductName = "華爲", UnitPrice = 2000 }, new Product { ProductName = "小米", UnitPrice = 1000 } } }; context.Categories.Add(category); context.SaveChanges(); }
exec sp_executesql N'SET NOCOUNT ON; INSERT INTO [Category] ([CategoryName]) VALUES (@p0); SELECT [CategoryID] FROM [Category] WHERE @@ROWCOUNT = 1 AND [CategoryID] = scope_identity(); ',N'@p0 nvarchar(4000)',@p0=N'手機'
exec sp_executesql N'SET NOCOUNT ON; INSERT INTO [Product] ([CategoryID], [ProductName], [UnitPrice]) VALUES (@p0, @p1, @p2); SELECT [ProductID] FROM [Product] WHERE @@ROWCOUNT = 1 AND [ProductID] = scope_identity(); ',N'@p0 int,@p1 nvarchar(4000),@p2 decimal(4,0)',@p0=1,@p1=N'華爲',@p2=2000
exec sp_executesql N'SET NOCOUNT ON; INSERT INTO [Product] ([CategoryID], [ProductName], [UnitPrice]) VALUES (@p0, @p1, @p2); SELECT [ProductID] FROM [Product] WHERE @@ROWCOUNT = 1 AND [ProductID] = scope_identity(); ',N'@p0 int,@p1 nvarchar(4000),@p2 decimal(4,0)',@p0=1,@p1=N'小米',@p2=1000
若是從已由DbContext跟蹤的實體的導航屬性中引用新實體,則該實體將插入到數據庫中。get
using Microsoft.EntityFrameworkCore;
using (var context = new LibingContext()) { var category = context.Categories .Include(t => t.Products) .Where(t => t.CategoryID == 1) .FirstOrDefault(); category.Products.Add(new Product { ProductName = "VIVO", UnitPrice = 1500 }); context.SaveChanges(); }
SELECT TOP(1) [t].[CategoryID], [t].[CategoryName] FROM [Category] AS [t] WHERE [t].[CategoryID] = 1 ORDER BY [t].[CategoryID]
SELECT [t.Products].[ProductID], [t.Products].[CategoryID], [t.Products].[ProductName], [t.Products].[UnitPrice] FROM [Product] AS [t.Products] INNER JOIN ( SELECT TOP(1) [t0].[CategoryID] FROM [Category] AS [t0] WHERE [t0].[CategoryID] = 1 ORDER BY [t0].[CategoryID] ) AS [t1] ON [t.Products].[CategoryID] = [t1].[CategoryID] ORDER BY [t1].[CategoryID]
exec sp_executesql N'SET NOCOUNT ON; INSERT INTO [Product] ([CategoryID], [ProductName], [UnitPrice]) VALUES (@p0, @p1, @p2); SELECT [ProductID] FROM [Product] WHERE @@ROWCOUNT = 1 AND [ProductID] = scope_identity(); ',N'@p0 int,@p1 nvarchar(4000),@p2 decimal(4,0)',@p0=1,@p1=N'VIVO',@p2=1500
若是更改實體的導航屬性,則將對數據庫中的外鍵列進行相應的更改。it
using (var context = new LibingContext()) { var category = context.Categories.Find(2); var product = context.Products.Find(1); product.Category = category; context.SaveChanges(); }
exec sp_executesql N'SELECT TOP(1) [e].[CategoryID], [e].[CategoryName] FROM [Category] AS [e] WHERE [e].[CategoryID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=2
exec sp_executesql N'SELECT TOP(1) [e].[ProductID], [e].[CategoryID], [e].[ProductName], [e].[UnitPrice] FROM [Product] AS [e] WHERE [e].[ProductID] = @__get_Item_0',N'@__get_Item_0 int',@__get_Item_0=1
exec sp_executesql N'SET NOCOUNT ON; UPDATE [Product] SET [CategoryID] = @p0 WHERE [ProductID] = @p1; SELECT @@ROWCOUNT; ',N'@p1 int,@p0 int',@p1=1,@p0=2
能夠經過將引用導航設置爲 null 或從集合導航中刪除相關實體來刪除關係。
默認狀況下,對於必選關係,將配置級聯刪除行爲,並將從數據庫中刪除子實體/依賴實體。 對於可選關係,默認狀況下不會配置級聯刪除,但會將外鍵屬性設置爲 null。
using (var context = new LibingContext()) { var category = context.Categories .Include(t => t.Products) .Where(t => t.CategoryID == 1) .FirstOrDefault(); //category.Products.Remove(category.Products.FirstOrDefault()); category.Products = null; context.SaveChanges(); }
SELECT TOP(1) [t].[CategoryID], [t].[CategoryName] FROM [Category] AS [t] WHERE [t].[CategoryID] = 1 ORDER BY [t].[CategoryID]
SELECT [t.Products].[ProductID], [t.Products].[CategoryID], [t.Products].[ProductName], [t.Products].[UnitPrice] FROM [Product] AS [t.Products] INNER JOIN ( SELECT TOP(1) [t0].[CategoryID] FROM [Category] AS [t0] WHERE [t0].[CategoryID] = 1 ORDER BY [t0].[CategoryID] ) AS [t1] ON [t.Products].[CategoryID] = [t1].[CategoryID] ORDER BY [t1].[CategoryID]
exec sp_executesql N'SET NOCOUNT ON; DELETE FROM [Product] WHERE [ProductID] = @p0; SELECT @@ROWCOUNT; ',N'@p0 int',@p0=2
exec sp_executesql N'SET NOCOUNT ON; DELETE FROM [Product] WHERE [ProductID] = @p0; SELECT @@ROWCOUNT; ',N'@p0 int',@p0=3