Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same key value for is already being tracked

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; }
    }
like image 293
streamc Avatar asked Jan 15 '18 06:01

streamc


People also ask

Why can't I track an entity with the same key value?

same key value for is already being tracked. 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.

Why is the entity type 'address' not being tracked?

When attaching existing entities, ensure that only one entity instance with a given key value is attached. #1971 The instance of entity type 'Address' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.

Why Entity Framework instance cannot be tracked?

Entity Framework core - The instance of entity type cannot be tracked because another instance of this type with the same key is already being tracked 6 The instance of entity type ... cannot be tracked because another instance of this type with the same key is already being tracked 35

Why can't the entity type'bookmark'be tracked?

The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked Hot Network Questions Do delisted games on Steam count as perfected games if I would 100% them now?


1 Answers

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!

like image 81
Georg Patscheider Avatar answered Oct 16 '22 18:10

Georg Patscheider