Daily Archives: February 12, 2010

Entity Framework Graphs, Detaching and Lazy Loading

If you have a graph of entities which are being managed by an ObjectContext, e.g, a LineItem and its Product, and for one reason or another, you detach the Product from the context, lazy loading will query for that Product again if you happen to call LineItem.Product. Now you will have two instances of that product in memory and one of them is being change-tracked.

It’s understandable behavior, but I know that someone will get bitten by this someday.

It’s more likely to happen when you are exploring how relationships work and doing weird things like I do. But it’s also possible that you might do it in a production app as well if you don’t know what you’re doing.