Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After second SaveChanges() EF loses added new dependents #5734

Closed
AlexBilonog opened this issue Jun 11, 2016 · 4 comments
Closed

After second SaveChanges() EF loses added new dependents #5734

AlexBilonog opened this issue Jun 11, 2016 · 4 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@AlexBilonog
Copy link

AlexBilonog commented Jun 11, 2016

After second SaveChanges() EF loses added new dependents:

using (var db = new TheContext())
{
   var user = db.Set<User>().AsQueryable().First();
   var userDepartment = new UserDepartment { DepartmentID = 1 };
   user.UserDepartments.Add(userDepartment);
   db.SaveChanges(); // After this line: user.UserDepartments: Count = 1 (OK)
   db.SaveChanges(); // After this line: user.UserDepartments: Count = 0 (Wrong)
   db.SaveChanges(); // By executing this line exception: The association between entity types 'User' and 'UserDepartment' has been severed but the foreign key for this relationship cannot be set to null
}

EF Core version: 1.0.0-rc3-21338
Operating system: Windows 10
Visual Studio version: VS 2015 last updates
StackTrace:

   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at EFGetStarted.ConsoleApp.Program.Main() in C:\Work\Console4\EFGetStarted.ConsoleApp\Program.cs:line 21
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
@ajcvickers
Copy link
Contributor

@AlexBilonog Can you post the code for the entity types User and UserDepartment and any code you have in OnConfiguring?

Note for triage: I have been able to reproduce the issue where the navigation properties get corrupted, but so far not the conceptual null exception.

@AlexBilonog
Copy link
Author

AlexBilonog commented Jun 13, 2016

Unfortunately I can provide code only next Monday. But I can add some details. UserID is NOT NULL FK. On Delete and On Update setting is Restrict. DB is SqlServer 2012.

ajcvickers added a commit that referenced this issue Jun 13, 2016
Issue #5734

When we encounter an untracked entity in a navigation we save it for later fixup, but we were not adding these entities to the snapshot for the navigation. This later caused change detection to be confused, which resulted in the entities being removed from the navigation.
@rowanmiller rowanmiller added this to the 1.0.0 milestone Jun 13, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 6, 2016
@dc-m
Copy link

dc-m commented Feb 3, 2017

I am getting this issue on 1.1.0 exact same code style as originally posted and same error.

@ajcvickers
Copy link
Contributor

@dc-m Please open a new issue and include a project or the code needed to reproduce what you are seeing.

@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants