last day (14 days later) » 

9:33 AM
0
Q: Correct Way to Change Entity Framework Relationship

Jonathan WoodGiven a database schema with two tables: Company and Employee, where the Employee table has a foreign key to the Company table, what is the correct code to change the company a particular employee is associated with? Method 1: company1.Employees.Remove(employee); company2.Employees.Add(employee...

 
I assume the relationship of Company-Employee here is one-many, and looks like it should work for the method2. At least in simple test it works. If the method2 does not work for you, you should provide more info on the scenario here.
I would say that in a simple test all these methods work (tested on one-many relationship, database-first, lazy-loading enabled, change-tracking enabled). So saying that one of them does not work is not fair at all. Some specific context should be provided so that it's reproducible.
 
You need to check your schema. You have something wrong somewhere. All these methods should work. Your first method shouldn't be loading the whole list. The best EF way is method 2 (it is very readable and does the job), although some people like to keep thinking the SQL way so they prefer method 3.
 
@RacilHilan: What do you mean that the first method shouldn't be loading the whole list? What makes you say that? The code calls a method on the company1.Employees collection. How can you call a method on a collection unless that collection is resolved by retrieving it from the database?
 
I can see you show an exception in the method2, for method3 it does not make any change to your db. So do you mean using method2 can throw exception and you want something that cannot or should never throw exception?
If you're careful enough all of them should not throw exception, otherwise all of them can do. Please look at what you asked in your question. It appears at first as a general question but it looks really specific to your problem. At least some explanation should be added for why you don't like/use this or that. e.g: in Method 2, instead of saying Problem:, you can write Possible problem:. Currently it looks really like that the method 2 always has that problem.
So do you mean your problem is specific? not general? so it actually requires code. How could everyone here debug that without code?
I totally don't understand why you could think someone can help about this. You talked about exception but others have to guess how those exception could be thrown (even googling is better). All 3 methods are totally fine but looks like you want some others to help improve just the first one. So you should have not proposed the other 2 methods which do not work for you. Or you want some others could explain and help find out how to make the method 2 work? That's impossible without any code.
BTW, in a simple test you can disable lazy-loading and just do this company2.Employees.Add(employee); for the method1, the employee will be auto moved to company2 (and of course removed from company1). Of course I'm talking about simple test, when it comes to a specific code, no one could suggest anything without the original code.
I asked this Or you want some others could explain and help find out how to make the method 2 work?, and you did not notice that?
@JonathanWood I checked the result in database.
I meant I checked the result after saving changes. For the loading, disabling lazy-loading will surely prevent the Employees from being loaded automatically when you access it. Looks like you've never heard about lazy loading?
I'm not so stupid that to check the result in db to know whether all of the related rows were loaded or not. I meant I check the result to know whether the employee A was moved from company A to company B.
So you mean we have no way to prevent employees from being loaded when accessing like this company.Employees? Please learn about EF more. You're obviously a newbiew in EF.
Please learn more about EF, you're a bit amateur. You can just ask another question about that specific issue. How to prevent entties from being auto-loaded? or even google for that.
So again do you really know about Lazy loading, please?
I did not answer to you because if you knew about Lazy loading, you would have understood what I meant, disabling lazy loading is really simple, once being disabled the collection will be empty after being accessed (debugging can even show that).
I've now been told by more than one person that my Method 1 does not load all related employees. But I have run multiple tests on this that show that it does - you did not even disable lazy-loading, so of course it will load all the related entities. I'm now not interested in your problem. This is my last comment to your question. I don't feel like I need to delete all my comments.
 
If Method 2 is throwing an error, and Method 3 doesn't change the DB, then this is a strong indication that you are using more than one DbContext. in the case of Method 2, you can't add company2 from a different context to the employee, it would see it as an add instead of a change. In case of Method 3, your saveChanges method isn't invoking the context that the employee is living in.
 
9:33 AM
@Claies: Regarding Method 2, the best I could see from researching that error is that it seems to occur when the context sees that an entity is used in two relationships where only one is allowed. That would seem to rule out different contexts.
 
@JonathanWood no, the opposite is true; if the context knows about company1, it will understand it should remove company1 before adding company2, if it doesn't know about company1, then it won't try to remove it, and the add of company2 will fail. You could try to explicitly Include Employee.Company before trying to replace it, but this still all points to something not 100% correct in your architecture. Are these objects loaded from a Repository/UoW by chance?
 
Method 3 should be the most efficient one by far. But I don't understand why the update doesn't seem to persist. It should be very simple. You should show more surrounding code for us to be able to analyze that. By the way, if you disable lazy loading it's true that method 1 doesn't load all Employees. With lazy loading, it does, for both companies. (With EF 6 that is. IIRC, in older EF versions collections had to be loaded to modify them).
 
@GertArnold: I think it is related to the fact that the context already holds a Company entity that is linked to the same Employee. That would explain the error with my Method 2, and perhaps that takes priority over setting the ID in my Method 3. Yes, if lazy loading is disabled, my Method 1 doesn't load any Employees. Unfortunately, my setup has a number of restraints and I can't easily access the context to delete the existing entity or modify lazy loading. I am looking into that.
 
No, it must be some thing else. It should work even if you have the whole database loaded into the context. I suggest you open a new question focusing on that specific problem, showing more details of the architecture in which this happens.
 

last day (14 days later) »