views:

292

answers:

1

I am a pretty much a newbie to Entity Framework (specifically version 4) and I am trying to understand an issue I have.

I have an ASP.NET MVC2 project I am using with Entity Framework 4 and a Repository class. I have a simple foreign key relationship that I have defined. When I run my MVC project I am able to load the MVC form and update the foreign key relationship by setting Foreign Key ID on my entity. I do this by calling the OnPropertyIDChanging in a partial class and setting the EntityKey like so

this.ManufacturerReference.EntityKey = new System.Data.EntityKey("MachinesEntities.Manufacturers", "ManufacturerId", value);

I have created a second project which is a command line project I use to load the initial data for the model. However, when I call into the Repository object to add the entity I get a cannot insert Null values into the child entity table (Manufacturer). If I explicitly set a reference on the parent object (Product) to the child object (Manufacturer) calling my repository to get a manufacturer object and setting the product's manufacturer object I get an exception stating

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I assume this is a result of having a global reference to the Entity context on each of my repository objects, and as a result I have an open context for the Manufacturer and the product.

In doing some research, I have seen that by loading the object from a query, that the object relationships are explicitly created via a RelationshipEntry object. This does not happen when I am creating the objects in my command line application. What is the way to get around this? I assume I will have the same issue if I try and create a Web Service since I am creating the object without calling SQL first.

Secondly, is there a best practice on managing the context? Is it better to have the context created and disposed within each repository method, a global reference in each of the repository objects or a context that gets passed to each repository constructor?

Thanks!

John

+3  A: 

Secondly, is there a best practice on managing the context? Is it better to have the context created and disposed within each repository method, a global reference in each of the repository objects or a context that gets passed to each repository constructor?

Actually, this is the important problem. Fix this and the other one should go away.

The context should be a unit of work. So for your web app, that will be one request. There should be exactly one context per unit of work. Use DI to share it amongst all code which participates in that unit of work, probably via your repositories.

For your command-line app, decide what the unit of work is.

This message:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

...just means, "You can't relate two entities which belong to different contexts. The real issue here is having two different contexts alive concurrently at all. This is not impossible, but so tricky to get right I consider it an "experts-only" technique.

This hack:

this.ManufacturerReference.EntityKey = new System.Data.EntityKey("MachinesEntities.Manufacturers", "ManufacturerId", value);

...isn't necessary in EF 4 anymore. In EF 4 you can generate your model with FK associations, and then just do:

this.ManufacturerId = value;
Craig Stuntz
Maybe I have an issue with how my Foreign Key is setup. I decided to try an implement the Model First approach and setup the foreign key that way (instead of via the database). However if I didn't set the EntityKey then I would get the insert null error for the child relationship. It could be another symptom of how the Context is being managed.
John Ptacek
[This post](http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx) explains how to set up an FK association with model first.
Craig Stuntz