I know I’ve seen this but I cant find it anywhere.
I have a couple objects with a many-to-many relationship: Person and Department. A person can have many departments and vice versa.
I thought it was:
var person = //query user
var d = new Department();
d.Id = 123;
person.Departments.add(d);
This creates a new department in the database and links them but that isnt what I want. The department already exists. I just want to create the relationship. How can I do this without having to requery the database to get an instance of the department?
Try this:
This shouldn’t query the database until
dbContext.SaveChanges()is called as deferred querying is used. However, if that doesn’t work, you can try updating the mapping entity directly instead: