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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:34:49+00:00 2026-05-27T22:34:49+00:00

My domain model uses EntityFramework 4.1 (and I’m using code-first) to update my DB

  • 0

My domain model uses EntityFramework 4.1 (and I’m using code-first) to update my DB with linked entities. I have a Member table and then a many-to-many MemberPositions table. The problem is that when I attempt to perform an update on a member with changed permissions, the code throws a run-time error of “An entity object cannot be referenced by multiple instances of IEntityChangeTracker” when I set the following from my repository class:

_context.Entry(entity).State = EntityState.Modified

Now I have read the posts at the following links, but they look outdated and some of the code doesn’t work in EF 4.1 / MVC3:

  • ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker
  • http://samscode.com/index.php/2009/12/making-entity-framework-v1-work-part-1-datacontext-lifetime-management/

So my questions are:

  1. What do I need to do to get this code working for EF 4.1 and MVC 3 (e.g. HttpContext.Current[“myDBEntities”] can’t be indexed using today’s framework)
  2. More importantly, where would this code need to reside? I can’t seem to figure out where to put the “datacontext per user per request” method or class as described in the first link. It doesn’t make sense in my domain project, because it does’t have access to the the HttpContext, so if it’s in my web project, how should I pass it to the domain?

To help you, below is a stripped down version of my domain model:

public class Entity
{
  public int Id { get; set; }
}

public class Member : Entity
{
  public string Name { get; set; }
  public virtual List<MemberPosition> Positions { get; set; }
}

public class MemberPosition : Entity
{
  public string Name { get; set; }
}

public class EfDbContext : DbContext
{
  public DbSet<Member> Members { get; set; }
  public DbSet<MemberPosition> MemberPositions { get; set; }
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Configurations.Add(new MemberMap());
    modelBuilder.Configurations.Add(new MemberPositionMap());
  }
}

public class MemberMap : EntityTypeConfiguration<Member>
{
  public MemberMap()
  {
    ToTable("Members");
    HasMany(m => m.Positions).WithMany().Map(
      m => m.ToTable("Member_MemberPositions").MapLeftKey("MemberId").MapRightKey("PositionId"));
  }
}

public class MemberPositionMap : EntityTypeConfiguration<MemberPosition>
{
  public MemberPositionMap()
  {
    ToTable("MemberPositions");
    Property(x => x.Name).IsRequired().HasMaxLength(100);
  }
}

public interface IRepository<TEntity>
{
  bool Update(TEntity entity);
}

public class Repository<TEntity> : IRepository<TEntity> where TEntity : Entity
{
  private readonly EfDbContext _context;
  private readonly DbSet<TEntity> _dbSet;

  public Repository(EfDbContext context)
  {
    _context = context;
    _dbSet = _context.Set<TEntity>();
  }

  public bool Update(TEntity entity)
  {
    _context.Entry(entity).State = EntityState.Modified;
    _context.SaveChanges();
    return true;
  }
}

And, finally, a stripped down version of the Edit method in my MemberController:

public class MemberController : Controller
{
  [HttpPost]
  public ActionResult Edit(MemberDetailViewModel memberDetailViewModel)
  {
    if (ModelState.IsValid)
    {
      var updatedMember = // Gets the member data from the view model...
      var memberRepository = // Creates a Repository<Member>;
      memberRepository.Update(updatedMember);
      return // blah... blah... blah...
    }
  }
}

Any suggestions are appreciated!

EDIT: THIS IS THE SOLUTION (IT’S EASY!)

In the App_Start.NinjectMVC3 controller, use the following code (make sure that InRequestScope) is present. In my original code, it wasn’t, hence it wasn’t working…

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).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-05-27T22:34:49+00:00Added an answer on May 27, 2026 at 10:34 pm

    As Slauma stated (and as included in my original post as an edit), here’s the solution.

    In the App_Start.NinjectMVC3 controller, use the following code (make sure that InRequestScope) is present. In my original code, it wasn’t, hence it wasn’t working…

    private static void RegisterServices(IKernel kernel)
    {
      kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been developing an iphone application using a domain model, and have put
We're using interfaces to represent entity classes in our domain model. We have concrete
I have a persistence ignorant domain model that uses abstract repositories to load domain
My domain model is using System.Net.Uri to represent URLs, and System.Drawing.Color to represent colors.
My domain model is this: we have a bunch of schools as the root
We have a simple domain model: Contact, TelephoneNumber and ContactRepository. Contact is entity, it
We have our domain model declared in rusty old hbm files, we wish to
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
I have an application A with a domain-model which is mapped to a database
we have this application which uses cross app domain (2 app domains in the

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.