Get methods with Include leads to (potentially massive) data loss!
My database has an Enterprise entity with a one-to-many relationship with the Site entity, which has a one-to-many relationship with the Program entity.
The use of an Include() in the DbContext get method results in all children of the parent entity being deleted. Get methods that do not have Include() perform normally.
For example, calling:
DbContext.Enterprises.Include(x => x.Sites).ToList();
Followed by
DbContext.SaveChanges();
Will delete ALL the Site entities in the database!
No errors occur and the log clearly shows the delete SQL statement. Here is the log output from context initialization to end:
info: Microsoft.EntityFrameworkCore.Infrastructure[100403]
Entity Framework Core 2.0.0-rtm-26452 initialized 'EtoDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=Derp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
info: Microsoft.EntityFrameworkCore.Database.Command[200101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x].[EnterpriseId], [x].[EtoBaseUrl], [x].[IsActive], [x].[Name]
FROM [Enterprise] AS [x]
ORDER BY [x].[EnterpriseId]
info: Microsoft.EntityFrameworkCore.Database.Command[200101]
Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x.Sites].[SiteId], [x.Sites].[EnterpriseId], [x.Sites].[EtoSiteId], [x.Sites].[IsActive], [x.Sites].[Name]
FROM [Site] AS [x.Sites]
INNER JOIN (
SELECT [x0].[EnterpriseId]
FROM [Enterprise] AS [x0]
) AS [t] ON [x.Sites].[EnterpriseId] = [t].[EnterpriseId]
ORDER BY [t].[EnterpriseId]
info: Microsoft.EntityFrameworkCore.Database.Command[200101]
Executed DbCommand (33ms) [Parameters=[@p0='?'], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
DELETE FROM [Site]
WHERE [SiteId] = @p0;
SELECT @@ROWCOUNT;
System.Collections.Generic.List`1[Derp.Ef.Enterprise] programs found. 1 records modified.
Steps to reproduce
I have created a console app in a repo that exhibits the problem here: https://github.com/Ketrex/EfCoreBug Just update the connection string in appconfig and point it to a valid SQL server and observe. Migrations are included and set to run on application startup.
In the Program.cs file there are four ServiceCollection scopes. The first scope runs migrations, the second scope seeds the database with dummy records, the third scope runs a DbContext GET with no includes to demonstrate no records are deleted and the GET returns normally. The fourth scope executes a GET with an Include, so the delete behavior can be observed. Console logging is setup by default.
Further technical details
EF Core version: 2.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer, SQL Server 2016 Standard Operating system: Windows 10 IDE: Visual Studio 2017 Enterprise
This is user code error.
In your Site.cs you have this property.
public IList<Program> Programs => new List<Program>();
Which means everytime you access Programs, you would get new empty list of Program. Therefore when SaveChanges tries to detect changes, it fetches the value of Site.Programs which is empty list different from List of ` program which was loaded, hence it deletes the Program.
Similar issues are there in other classes.
Thanks @smitpatel, not sure how I missed that!