-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Make many to many relationship on same domain model #34224
Comments
@GMCadiom There are a couple of things going on here. First, you are re-using the same collection instance for both the emp.EmpManagerMangers = new List<EmpManager>()
{
new EmpManager() { MangerId = 4 }
};
emp.EmpManagerEmps = emp.EmpManagerMangers; Each navigation must have its own collection so it can be updated independently to other navigations. Second, the foreign key property associated with If you're instead trying to set the FK on the other side of the relationship, then that FK property is emp.EmpManagerMangers = new List<EmpManager>()
{
new EmpManager() { EmpId = 4 }
};
// No need to set the inverse navigation
// emp.EmpManagerEmps = emp.EmpManagerMangers;
db.SaveChanges(); (You may want to rename your FK properties.) Results in the following entities before SaveChanges:
And after SaveChanges:
|
@ajcvickers |
i have those domain models
and the dbcontext is :
My test code is :
On this example, I just want to make for each employee multiple managers and each manager multiple employees.
but after call SaveChanges() method I found on database that the result is :
The EmpId and MangerId equals the current employee model,
if I am on emp with id = 1 and set for him 2 managers {3,4}
the result became the 2 managers be {1,1} (the current emp id)
in the attachment the test project that i test this issue,
ConsoleApp1.zip
Include provider and version information
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.9.3)
The text was updated successfully, but these errors were encountered: