same key value for is already being tracked

c# entity-framework-6

Question

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

I made a project with Worker (ID and other columns) And Project (ID and other columns) Entity and ProjectAssignment (many Project ID to many Workers) Crossbound. But error in Application Insights appears when I try to fetch ProjectAssignment data. Strange that id does not appear earlier.

Here it is 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
4
1/15/2018 7:04:51 AM

Accepted Answer

Read the error message carefully:

The instance of entity type 'ProjectAssignment' cannot be tracked because another instance with the same key value for {'ProjectID'} is already being tracked.

So the problem is that we got two or more ProjectAssignment with the same ProjectID.

If we look at the code we see that we assign the same ProjectID twice:

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

The root cause seems to be that ProjectID was modeled as Primary Key instead of as Foreign Key. Primary Keys must be unique. What you probably want is a composite primary key consisting of ProjectID and WorkerID.

Looking at the ProjectContext in your Github project, this composite key is already in place:

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

But it conflicts with the [Key] annotation on ProjectAssignment.ProjectID. Remove this [Key] attribute!

8
1/15/2018 9:59:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow