1

The way I currently design systems is that each repository has its own database context. I dependency inject the connection string for each repository.

But lets say 1 page calls 2 or more (n) repositories, this means there are n seperate calls going to the database. Using ORMs like entity framework with lazy loading, is it possible that my queries can be merged at the end of the repository level into 1 call if I share 1 database context?

How do I accomplish this? Could someone give me a basic outline/structure of how I'd use 1 single context for all repositories? Thanks.

1

1 Answer 1

1

With entity framework your queries will never be merged to single call. That is limitation of current EF implementation. But having single context per one http reqeuest (best practice) is way to go. Instead of injecting query string inject whole context.

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

4 Comments

So I would make a base repository and make the context static?
No!!! You need to indtantiate single context instance per request and pass it to all constructors of required repositories. This is usually handled by IoC container.
I'm using ninject for my IoC, that means I'll have to reference the entity framework dll in my web project?
Only if you are using fluent api to configure the container. To avoid exposing ef features directly to ui people use second pattern called unit of work.

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.