c# - solve error "The entity cannot be tracked" - Stack Overflow

admin2025-04-28  3

Entities defectmelmaster and defectmeleffectivity have the same primary key.

After forming the lists, I would like to insert these separate lists for defectmelmaster and defectmeleffectivity.

However, after defectmelmaster AddRangeAsync succeeds, defectmeleffectivity AddRange throws an error:

Entity cannot be tracked because same instance ...

Service.cs is where the lists are formed and repo insert methods are called:

foreach (var val in entities)
{
      var defectMelMasterQuery = FormatDefectMelMasterQuery(val, userRegisterId);
      var defectMelEffectivityQuery = FormatDefectMelEffectivityQuery(val, userRegisterId);
}
           
defectMelMasterQueryList.Add(defectMelMasterQuery);
defectMelEffectivityQueryList.Add(defectMelEffectivityQuery);

await _defectMelMasterRepository.InsertDefectMelMaster(defectMelMasterQueryList, cancellationToken);
await _defectMelEffectivityRepo.InsertDefectMelEffectivity(defectMelEffectivityQueryList, cancellationToken); //throws the error

DefectMelEffectivityRepo.cs is where the AddRangeAsync is called and where the error is thrown:

//DefectMelEffectivityRepo

await _dbContext.Set<DefectMelEffectivity>().
            AddRangeAsync(defectMelEffectivityQueryList, cancellationToken);

await _dbContext.SaveChangesAsync(cancellationToken);

Entity class defectmeleffectivityentity.cs:

public void Configure(EntityTypeBuilder<DefectMelEffectivity> builder)
    {
        builder.ToTable("DEFECT_MEL_EFFECTIVITY", schema: "XXX");
        
        builder.Ignore(x => x.Id);
        
        builder.HasKey(f => new {f.AcType, f.AcSeries, 
            f.MelNumber, f.MelSub, f.Ac, f.Company});
    }

Entity class defectmelmasterentity.cs:

public void Configure(EntityTypeBuilder<DefectMelMaster> builder)
    {
        builder.ToTable("DEFECT_MEL_MASTER", schema: "XXX");
        builder.Ignore(x => x.Id);
        builder.HasKey(f => new {f.AcType, f.AcSeries, 
            f.MelNumber, f.MelSub, f.Company});
    }

How can I solve this?

Entities defectmelmaster and defectmeleffectivity have the same primary key.

After forming the lists, I would like to insert these separate lists for defectmelmaster and defectmeleffectivity.

However, after defectmelmaster AddRangeAsync succeeds, defectmeleffectivity AddRange throws an error:

Entity cannot be tracked because same instance ...

Service.cs is where the lists are formed and repo insert methods are called:

foreach (var val in entities)
{
      var defectMelMasterQuery = FormatDefectMelMasterQuery(val, userRegisterId);
      var defectMelEffectivityQuery = FormatDefectMelEffectivityQuery(val, userRegisterId);
}
           
defectMelMasterQueryList.Add(defectMelMasterQuery);
defectMelEffectivityQueryList.Add(defectMelEffectivityQuery);

await _defectMelMasterRepository.InsertDefectMelMaster(defectMelMasterQueryList, cancellationToken);
await _defectMelEffectivityRepo.InsertDefectMelEffectivity(defectMelEffectivityQueryList, cancellationToken); //throws the error

DefectMelEffectivityRepo.cs is where the AddRangeAsync is called and where the error is thrown:

//DefectMelEffectivityRepo

await _dbContext.Set<DefectMelEffectivity>().
            AddRangeAsync(defectMelEffectivityQueryList, cancellationToken);

await _dbContext.SaveChangesAsync(cancellationToken);

Entity class defectmeleffectivityentity.cs:

public void Configure(EntityTypeBuilder<DefectMelEffectivity> builder)
    {
        builder.ToTable("DEFECT_MEL_EFFECTIVITY", schema: "XXX");
        
        builder.Ignore(x => x.Id);
        
        builder.HasKey(f => new {f.AcType, f.AcSeries, 
            f.MelNumber, f.MelSub, f.Ac, f.Company});
    }

Entity class defectmelmasterentity.cs:

public void Configure(EntityTypeBuilder<DefectMelMaster> builder)
    {
        builder.ToTable("DEFECT_MEL_MASTER", schema: "XXX");
        builder.Ignore(x => x.Id);
        builder.HasKey(f => new {f.AcType, f.AcSeries, 
            f.MelNumber, f.MelSub, f.Company});
    }

How can I solve this?

Share Improve this question edited Jan 10 at 10:19 philipxy 15.2k6 gold badges43 silver badges97 bronze badges asked Jan 9 at 18:39 BegummBegumm 691 gold badge2 silver badges11 bronze badges 5
  • @topsail sorry I've edited my question. I hope it is more clear now. – Begumm Commented Jan 9 at 18:58
  • What do you mean by Same primary key?? can you share your entity classes? – sa-es-ir Commented Jan 9 at 20:31
  • The error means that the DbContext has already loaded at least one of these entities with the same ID that you are trying to insert. Where does this collection of "entities" you are trying to insert come from? The fact that the two entities share the same PK is no issue, but you have run into a scenario where one of the second entities is attempting to insert a duplicate. Even if the Context was fresh and tracking nothing you would almost certainly get a duplicate PK error on Save. – Steve Py Commented Jan 9 at 20:59
  • I've added entity classes. – Begumm Commented Jan 10 at 7:12
  • What is the solution then because same entry to different tables will be added? – Begumm Commented Jan 10 at 7:17
Add a comment  | 

1 Answer 1

Reset to default 1

You could try changing state of the entity to Modified:

_context.Entry(defectmeleffectivity).State = EntityState.Modified; // or  defectmelmaster, whichever is inserted second

This, of course, would need to be implemented correctly in repository, where you have access to DbContext.

转载请注明原文地址:http://anycun.com/QandA/1745779970a91188.html