我正在尝试使用实体框架代码优先来创建ASP.NET MVC Web应用程序。 我有两个简单的模型使用DbContext中的同一个键保留多个对象
public class Assignment
{
public int Id { get; set; }
[ForeignKey("Initiator")]
public int InititatorId { get; set; }
public virtual User Initiator { get; set; }
public virtual List<User> Debtors { get; set; }
public virtual List<User> Responsibles { get; set; }
public Assignment()
{
Debtors = new List<User>();
Responsibles = new List<User>();
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
和我的DbContext看起来像
public class AssignmentContext : DbContext
{
public DbSet<Assignment> Assignments { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Assignment>()
.HasRequired(r => r.Initiator)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Assignment>()
.HasMany(m => m.Responsibles)
.WithMany()
.Map(m =>
{
m.MapLeftKey("AssignmentId");
m.MapRightKey("UserId(Responsible)");
m.ToTable("AssignmentResponsibles");
});
modelBuilder.Entity<Assignment>()
.HasMany(m => m.Debtors)
.WithMany()
.Map(m =>
{
m.MapLeftKey("AssignmentId");
m.MapRightKey("UserId(Debtor)");
m.ToTable("AssignmentDebtors");
});
}
有没有办法保持相同的用户实例在列表债务人和Responsibles?
因为我在尝试保存更改时遇到错误。
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
我需要这样做,因为它是应用程序逻辑所要求的。
在此先感谢。
更新:
上。这里HttpPost错误出现(控制台应用样品中的相同的错误):从视图
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Text,CreationDate,InititatorId,Debtors,Responsibles")] Assignment assignment)
{
if (ModelState.IsValid)
{
assignment.Debtors.RemoveAll(r => r.Name == "Roman");
db.Entry(assignment).State = EntityState.Modified; // HERE!
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.InititatorId = new SelectList(db.Users, "Id", "Name", assignment.InititatorId);
return View(assignment);
}
借方和Responsbiles元件由隐藏的输入通过。
我无法重现的错误,你确定这就是你拥有的代码? –
@YuliamChandra是的,当我们创建DB时,错误不会出现。但是当我们试图改变某些东西时(在Debtor列表中删除一个条目),我们在代码'db.Entry(assignment).State = EntityState.Modified;' –
@YuliamChandra得到错误我不知道它或不被允许,但是我将自己发布链接到存档,其中包含我的项目的两个版本(** console和asp.net-mvc **)[** from googledrive **](https://drive.google.com/file/d/0B4a4mpg7rb4AUnpUMGJvVzlxM00/edit?usp = sharing) –