具有多对多更新的实体框架 4.1 会产生“实体对象不能被 IEntityChangeTracker 的多个实例引用”错误

发布于 2024-12-24 17:27:00 字数 3696 浏览 0 评论 0原文

我的域模型使用 EntityFramework 4.1(并且我使用代码优先)来使用链接实体更新我的数据库。我有一个 Member 表,然后是一个多对多 MemberPositions 表。问题是,当我尝试对具有更改权限的成员执行更新时,当我设置时,代码会抛出“实体对象不能被 IEntityChangeTracker 的多个实例引用”的运行时错误我的存储库类中的以下内容:

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

现在我已阅读以下链接中的帖子,但它们看起来已经过时,并且某些代码在 EF 4.1 / MVC3 中不起作用:

所以我的问题是:

  1. 什么我需要做什么才能让这段代码适用于 EF 4.1 和 MVC 3(例如 HttpContext.Current["myDBEntities"] 无法使用当今的框架进行索引)
  2. 更重要的是,这段代码需要驻留在哪里?< /strong> 我似乎无法弄清楚在哪里放置第一个链接中描述的“每个用户每个请求的数据上下文”方法或类。这在我的域项目中没有意义,因为它无法访问 HttpContext,所以如果它在我的 Web 项目中,我应该如何将它传递到域?

为了帮助您,下面是我的域模型的精简版本:

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;
  }
}

最后,我的 MemberController 中的 Edit 方法的精简版本:

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...
    }
  }
}

感谢任何建议!

编辑:这就是解决方案(很简单!)

在 App_Start.NinjectMVC3 控制器中,使用以下代码(确保 InRequestScope)存在。在我的原始代码中,它不是,因此它不起作用......

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
}

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:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-12-31 17:27:00

正如 Slauma 所说(并作为编辑包含在我的原始帖子中),这就是解决方案。

在 App_Start.NinjectMVC3 控制器中,使用以下代码(确保 InRequestScope)存在。在我的原始代码中,它不是,因此它不起作用......

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文