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 7833623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:47:56+00:00 2026-06-02T12:47:56+00:00

I am using the following UnitOfWork class in order to get repositories for my

  • 0

I am using the following UnitOfWork class in order to get repositories for my entities by using generics.

public class UnitOfWork : IUnitOfWork
{
  private readonly EfDbContext _context;

  public UnitOfWork()
  {
    _context = new EfDbContext();
  }

  public IRepository<T> RepositoryFor<T>() where T : Entity
  {
    return new Repository<T>(_context);
  }

  public void Save()
  {
    _context.SaveChanges();
  }
}

However, I have run into some problems where I have two separate classes that instantiate the UnitOfWork independently for a single Entity, meaning that two repositories for that Entity are created. This causes problems with Entity Framework throwing “An entity object cannot be referenced by multiple instances of IEntityChangeTracker” errors when I attempt save the Entity.

I am thinking that I need to more closely follow the UOW pattern as described in this post by Tom Dykstra under the “Creating the Unit of Work Class” section where the repository is stored in a private field and reused if existing or created if null. The issue that I have with this is that I want my UOW to continue using generics – Repository<T> – and not have to declare private repository fields for all the entity types I have in my code.

How might I be able to store the fact that a repository of a given type has already been instantiated and to re-use that instead of creating a new instance of it?

— EDIT: Here’s the fix —

Using Ninject, I adjusted my bindings to only instantiate a single instance of both the repositories and UOW by specifying InRequestScope

kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
//kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope(); // Removed based on comment below
kernel.Bind(typeof(EfDbContext)).ToSelf().InRequestScope();
  • 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-06-02T12:48:03+00:00Added an answer on June 2, 2026 at 12:48 pm

    The problem is not in multiple repositories, but in multiple DbContext instances. EF tells you that you can’t add the same entity instance to multiple contexts at once. So sharing repositories won’t help. Moreover, having multiple UoW sharing singleton repositories is the road to hell. It will be very difficult to handle concurrency (EF isn’t thread-safe), state, etc.

    To fix the issue you should do either:

    1. share Units of Work so that you use the same instance of UoW (and DbContext) throughout a single business operation
    2. create copies of entity instances before adding them to another UoW.

    The choice depends on your specific situation.

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

Sidebar

Related Questions

Using following controller: class mydvbController extends Zend_Controller_Action { public function indexAction() { } public
Using following code I try to get updated list of checkbuttons' corresponding text values,
I am using following code to get bitmap from url. This function is used
i am using following code in android to get IMEI number of phone TelephonyManager
Am using following .net code to add objects to cache: public static void Add<T>(string
I have a model like the following: public class Employee { public Employee() {
I'm using following model to store info about pages: class Page(models.Model): title = models.TextField(blank
I'm using following snippet for saving content: private void writeToFile(NodeRef nodeRef, String content) throws
Using following regexp I can get if a string starts with a or b
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>

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.