-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked #6490
Comments
Notes for triage: This is happening because Toy.IdRow is being used as the FK for the one-to-many relationship with Child, but IdRow is also the PK. This is being done by convention--see full simplified repro below. This means that the property is getting its value via FK propagation, but this obviously doesn't work for a PK in a one-to-many relationship. Note that if the line Full simplified repro: public class RootContext : DbContext
{
public RootContext(DbContextOptions<RootContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>();
modelBuilder.Entity<Child>();
modelBuilder.Entity<Toy>();
//Remove cascading deletes
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
}
public class Program
{
public static void Main(string[] args)
{
var opt = new DbContextOptionsBuilder<RootContext>();
opt.UseSqlServer(
"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=ef7-test-db; Integrated Security=True;");
using (DbContext db = new RootContext(opt.Options))
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var parent = new Parent
{
Name = "Parent",
Childs = new List<Child>
{
new Child
{
Name = "Child1",
Toys = new List<Toy>
{
new Toy {Name = "Toy1"},
new Toy {Name = "Toy2"},
}
},
new Child {Name = "Child1"}
}
};
db.Add(parent);
db.SaveChanges();
}
}
}
public class Parent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdRow { get; set; }
public string Name { get; set; }
public ICollection<Child> Childs { get; set; }
}
public class Child
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdRow { get; set; }
public string Name { get; set; }
public ICollection<Toy> Toys { get; set; }
}
public class Toy
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdRow { get; set; }
public string Name { get; set; }
} |
@jarzimichal The workaround for this is to specify an FK property for the Toy class. There are various ways of doing this, but the easiest is just to add a property that will be recognized by convention as the FK. For example: public class Toy
{
public int ChildId { get; set; } // Will be used as the FK
public string Name { get; set; }
} |
Awesome! Thanks a lot. Cheers, |
Hi,
I'm getting this error when I try to Insert domain object containing 2 level deep sub objects:
I have shared my project here: https://github.com/jarzimichal/ef7-tracking-issue/tree/master/src/EF7
Here is my code:
I have investigated the entity in debugger and looks like EF assigned the same '-2147482647' value to IdRow property in both Toys objects.
The text was updated successfully, but these errors were encountered: