如何在c#ef5 db第一种方法中使用IntersectionTable更新实体?

时间:2017-04-27 10:01:01

标签: c# entity-framework many-to-many db-first

我创建了我的数据库,并开始使用EF5和DB First方法在c#中开发Web应用程序。我可以在自己的数据字段上修改我的实体,但是在更新关系时不能让它工作。一个简单的关系示例是Project< - ProjectCategoryIntersection - >类别

型号:

 public class Project
 {
  public TProject project { get; private set; }
  public List<string> Categories { get; set; }   
 }

 public partial class TProject  //generated table object 
 { 
   public virtual ICollection<TProjectCategoryIntersection> TProjectCategoryIntersection { get; set; }
 }

public partial class TProjectCategoryIntersection
{
    public int Id { get; set; }
    public int ProjectId { get; set; }
    public int ProjectCategoryId { get; set; }

    public virtual TProject T_Project { get; set; }
    public virtual TCategory T_ProjectCategory { get; set; }
}

保存:

 public void SaveProject(Project project)
 {
 var context = new ProjectManagementEntities();

 TProject projectToUpdate = new TProject();
 projectToUpdate.Id = project.Id;

 foreach (var category in project.Categories)
 {
    var cat = (from c in context.TProjectCategory
                where c.Name == category
                select c).FirstOrDefault();
                var inters = new TProjectCategoryIntersection() { ProjectCategoryId = cat.Id, ProjectId = project.project.Id, TProject = project.project, TProjectCategory = cat };
                projectToUpdate.TProjectCategoryIntersection.Add(inters);
 }

 var entry = context.Entry(projectToUpdate).State = EntityState.Modified; //throws exceptions
 context.SaveChanges();
}

异常:

Conflicting changes to the role 'TProject' of the relationship 'ProjectManagementModel.FK_TProjectCategoryIntersection_TProject' have been detected.

当我尝试将类别直接添加到项目对象时,我也收到multiple instances ChangeTracker异常:

project.project.TProjectCategoryIntersection.Add(inters);

我应该从模型中删除生成的表对象吗?

public class Project
 {
  public TProject project { get; private set; } //remove this?
  public List<string> Categories { get; set; }   
 }

解决方案

我最终删除了生成的表对象public TProject project { get; private set; }并将我的代码更改为:

 public void SaveProject(Project project)
 {

            var context = new ProjectManagementEntities();

            var projectToUpdate = context.T_Project.Find(project.Id);
            foreach (var item in projectToUpdate.T_ProjectCategoryIntersection.ToList())
            {
                var oldCat = context.T_ProjectCategoryIntersection.Find(item.Id);
                context.T_ProjectCategoryIntersection.Remove(oldCat);
            }

            foreach (var category in project.Categories)
            {
                var cat = (from c in context.T_ProjectCategory
                           where c.Name == category
                           select c).FirstOrDefault();
                var inters = new T_ProjectCategoryIntersection() { ProjectCategoryId = cat.Id, ProjectId = project.Id };

                context.T_ProjectCategoryIntersection.Add(inters);
            }
            //more code...
            context.Entry(projectToUpdate).State = EntityState.Modified;
            context.SaveChanges();
  }

1 个答案:

答案 0 :(得分:0)

当您使用对象的引用以及同一对象中的ID的Integer并更改它们时,会出现这种情况。当发生这种情况时,EF无法知道哪一个是正确的参考

尝试仅设置ID并为

等引用设置null
var inters = new TProjectCategoryIntersection() { ProjectCategoryId = cat.Id, 
ProjectId = project.project.Id};