已经跟踪了相同的键值

时间:2018-01-15 06:54:00

标签: c# entity-framework-6

System.InvalidOperationException: The instance of entity type 'ProjectAssignment' cannot be tracked because another instance with the same key value for {'ProjectID'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.St

我用Worker(ID和其他列)和Project(ID和其他列)Entity和ProjectAssignment(许多Project的许多Project ID)Crossbound创建了一个项目。但是当我尝试获取ProjectAssignment数据时,会出现Application Insights中的错误。奇怪的是,id不会更早出现。

这是https://github.com/Streamc/ContosoObserve1/tree/master/Proj_s

 context.Database.EnsureCreated();

  var ProjectAssignments = new ProjectAssignment[]
             {
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 1).ID,
                        WorkerID = Workers.Single( i => i.ID == 1).ID,
                    },
                     new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 1).ID,
                        WorkerID = Workers.Single( i => i.ID == 2).ID,
                    },
                     new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 1).ID,
                        WorkerID = Workers.Single( i => i.ID == 3).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 2).ID,
                        WorkerID = Workers.Single( i => i.ID == 1).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 3).ID,
                        WorkerID = Workers.Single( i => i.ID == 1).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 4).ID,
                        WorkerID = Workers.Single( i => i.ID == 1).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 2).ID,
                        WorkerID = Workers.Single( i => i.ID == 2).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 2).ID,
                        WorkerID = Workers.Single( i => i.ID == 3).ID,
                    },
                    new ProjectAssignment
                    {
                        ProjectID = Projects.Single( i => i.ID == 2).ID,
                        WorkerID = Workers.Single( i => i.ID == 4).ID,
                    }

             };
            foreach (ProjectAssignment ss in ProjectAssignments)
            {
                context.ProjectAssignments.Add(ss);
            }
            context.SaveChanges();



 public class ProjectAssignment
    {
        [Key]
        public int ProjectID { get; set; }
        public int WorkerID { get; set; }


        public Project Project { get; set; }
        public Worker Worker { get; set; }

    }
   public class Worker
    {
        public int ID { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Fathername { get; set; }
        public string email { get; set; }    
        public string Company_name { get; set; }
        public ICollection<ProjectAssignment> ProjectAssignment { get; set; }

    }
    public class Project
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Customer_Company { get; set; }
        public string Executive_Company { get; set; }
        public int ManagerID { get; set; }    
        public int WorkerID { get; set; }    
        public DateTime Begin_date { get; set; }
        public DateTime End_date { get; set; }
        public int Priority { get; set; }    
        public string Test_comment { get; set; }    
        public ICollection<ProjectAssignment> ProjectAssignment { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

仔细阅读错误消息:

  

实体类型的实例&#39; ProjectAssignment&#39;无法跟踪,因为已经跟踪了另一个具有{&#39; ProjectID&#39;}键值的实例。

所以问题是我们有两个或更多ProjectAssignment具有相同的ProjectID

如果我们查看代码,我们会看到我们分配了两次相同的ProjectID

 var ProjectAssignments = new ProjectAssignment[] {
     new ProjectAssignment {
         ProjectID = Projects.Single( i => i.ID == 1).ID, // ID == "1"
         WorkerID = Workers.Single( i => i.ID == 1).ID,
     },
     new ProjectAssignment {
         ProjectID = Projects.Single( i => i.ID == 1).ID, // ID == "1", again
         WorkerID = Workers.Single( i => i.ID == 2).ID,
     },
     // ...
 }

根本原因似乎是ProjectID被建模为主键而不是外键。主键必须是唯一的。您可能想要的是由ProjectIDWorkerID组成的composite primary key

查看Github项目中的ProjectContext,此组合密钥已经到位:

modelBuilder.Entity<ProjectAssignment>().HasKey(c => new { c.ProjectID, c.WorkerID });

但它与[Key]上的ProjectAssignment.ProjectID注释相冲突。删除此[Key]属性!