0

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.

2
  • 3
    Please define what you mean by strange behavior. Commented Apr 13, 2010 at 19:16
  • Already solved Darin, thanx. What I wanted to mean with Strange Behavior was that when I commited some changes to related entities occured some diferent types of exceptions. Besides that, I solved my problem with the method explained in the other answer, with httpcontext shared variable. Commented Apr 15, 2010 at 14:50

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.