Thiago
Thiago

Reputation: 39

Repository Pattern and Entity Framework

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.

Upvotes: 0

Views: 966

Answers (1)

Ian Mercer
Ian Mercer

Reputation: 39297

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

Upvotes: 2

Related Questions