Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 216269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:32:55+00:00 2026-05-11T18:32:55+00:00

Greetings, Trying to sort through the best way to provide access to my Entity

  • 0

Greetings,
Trying to sort through the best way to provide access to my Entity Manager while keeping the context open through the request to permit late loading. I am seeing a lot of examples like the following:

public class SomeController
{
     MyEntities entities = new MyEntities();
}

The problem I see with this setup is that if you have a layer of business classes that you want to make calls into, you end up having to pass the manager as a parameter to these methods, like so:

public static GetEntity(MyEntities entityManager, int id)
{
      return entityManager.Series.FirstOrDefault(s => s.SeriesId == id);
}

Obviously I am looking for a good, thread safe way, to provide the entityManager to the method without passing it. The way also needs to be unit testable, my previous attempts with putting it in Session did not work for unit tests.

I am actually looking for the recommended way of dealing with the Entity Framework in ASP .NET MVC for an enterprise level application.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:32:56+00:00Added an answer on May 11, 2026 at 6:32 pm

    You might want to look at the Repository pattern (here’s a write up of Repository with Linq to SQL).

    The basic idea would be that instead of creating a static class, you instantiate a version of the Repository. You can pass in your EntityManager as a parameter to the class in the constructor — or better yet, a factory that can create your EntityManager for the class so that it can do unit of work instantiation of the manager.

    For MVC I use a base controller class. In this class you could create your entity manager factory and make it a property of the class so deriving classes have access to it. Allow it to be injected from a constructor but created with the proper default if the instance passed in is null. Whenever a controller method needs to create a repository, it can use this instance to pass into the Repository so that it can create the manager required.

    In this way, you get rid of the static methods and allow mock instances to be used in your unit tests. By passing in a factory — which ought to create instances that implement interfaces, btw — you decouple your repository from the actual manager class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings! I am trying to find a way in Java to programmatically enable/disable a
Greetings! I am trying to find a way in Java to programmatically enable/disable a
Greetings I'm trying to find a way using Linq-to-SQL to get the first 15
Greetings, I'm trying to programmatically login through the provider in weblogic security realm from
Greetings I am trying to find a way to implement view only user group
Greetings, I'm trying to find a way to 'unbind' a socket from a particular
Greetings stackers, I'm trying to come up with the best database schema for an
Greetings from some noob trying to learn JQuery, I am attempting to make it
Greetings, I am trying to learn pointers in C, I simply want my addtwo
Greetings! I'm trying to have a UIWebView controlled from generated events. Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.