views:

238

answers:

1

I want to make an implementation with repository pattern with ASP.NET MVC 2 and Entity Framework but I have had some issues in the process.

First of all, I have 2 entities that has a relationship between them, like Order and Product. When I generate my dbml file it gaves me a class Order with a property that map a "ProductSet" and one class Product with a property that map wich Order that Product relates itself.

So I create my Repository pattern like IReporitory with the basic CRUD operations and inside my controllers I implement the ProductRepository or OrderRepository.

The problem occurs when I try to create Product and have to assign my Order on it, like ProductOne.Order = _orderRepository.Find(orderId);

That operation gave me some strange behavior and I can't find out what is wrong with it.

+1  A: 

The question is somewhat lacking in details but my guess is that you are using two separate ObjectContexts in your two repositories instead of one. You'll want to manage the lifetime of your ObjectContext to be scoped to a single web request and have only one ObjectContext for the lifetime of that web request cycle.

Google search for 'web scoped objectcontext' or 'objectcontext lifetime'.

e.g. http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx

Hightechrider
Thx for the info Hightechrider, That is exactly the problem, but I'm a newbie in repository Pattern and I don't know how to control this, maybe with a singleton pattern, I don't know. Does anyone have some example about can I do this?
I added a reference to an article that shows ways to manage ObjectContext lifetimes. Scoping it to the web request lifetime works well IMHO.
Hightechrider
Thx very much Hightechrider.