【问题标题】:How to update entity with IntersectionTable in c# ef5 db first approach?如何在 c# ef5 db first 方法中使用 IntersectionTable 更新实体?
【发布时间】:2017-04-27 10:01:00
【问题描述】:

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

型号:

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

【问题讨论】:

  • 试试context.Entry(projectToUpdate).State = System.Data.EntityState.Added;
  • 谢谢,但我之前试过...同样的例外。

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


【解决方案1】:

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

尝试仅设置 Ids 并为引用设置 null

var inters = new TProjectCategoryIntersection() { ProjectCategoryId = cat.Id, 
ProjectId = project.project.Id};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-11
    • 2014-08-14
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多