简体   繁体   English

EF Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值

[英]EF Core: The instance of entity type cannot be tracked because another instance with the same key value

Assume we have the two following database tables:假设我们有以下两个数据库表:

Foo:
> FooId (PK)
> FooName

Bar:
> BarId (PK, FK)
> Comment
> Other columns...

I have the following EF mapping:我有以下 EF 映射:

[Table("Foo")]
public class Foo
{
    [Key]
    public long FooId { get; set; }

    public string FooName { get; set; }

    // (0-n) relation
    // public List<Bar> Bars { get; set; }
}

[Table("Bar")]
public class Bar
{
    // PK/FK
    [Key, ForeignKey("Foo")]
    public long BarId { get; set; }

    public string Comment { get; set; }
}

The entity "Bar" has only a foreign key as primary key.实体“Bar”只有一个外键作为主键。 Whenever I try to insert new Bar entities like this:每当我尝试像这样插入新的 Bar 实体时:

var demoList = new List<Bar>();
// Populate demoList with random data
_context.Bars.AddRange(demoList);
_context.SaveChanges();

I'm getting this exception:我得到了这个例外:

'The instance of entity type 'Bar' cannot be tracked because another instance with the same key value for {'BarId'} 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.'

EF is considering that "BarId" must be unique as the property is marked as "Key", but it's a PK/FK in a one to many relation ("Foo" can have 0 or many "Bar"), what I'm missing here please? EF 正在考虑“BarId”必须是唯一的,因为该属性被标记为“Key”,但它是一对多关系中的 PK/FK(“Foo”可以有 0 个或多个“Bar”),我是什么请问这里不见了?

If Foo can have Zero or Many Bar s it is a One-To-Many relationship.如果Foo可以有 Zero 或 Many Bar ,则它是一对多关系。 You would typically create a key as both PrimaryKey and ForiegnKey if the relationship is One-to-Zero-or-One.如果关系是一对零或一,您通常会创建一个作为PrimaryKeyForiegnKey的键。 So per your requirement you models should be more like below:因此,根据您的要求,您的模型应该更像如下:

[Table("Foo")]
public class Foo
{
    [Key]
    public long FooId { get; set; }

    public string FooName { get; set; }

    public virtual List<Bar> Bars { get; set; }
}

[Table("Bar")]
public class Bar
{

    [Key]
    public long BarId { get; set; }

    public long FooId { get; set; }

    [ForeignKey("FooId")]
    public virtual Foo Foo { get; set; }

    public string Comment { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core(内存数据库)-'无法跟踪实体类型 X 的实例,因为另一个具有键值的实例 - EF Core (in-memory database) - 'The instance of entity type X cannot be tracked because another instance with the key value Entity framework core 无法跟踪实体类型的实例,因为另一个实例具有相同的键值 - Entity framework core The instance of entity type cannot be tracked because another instance with the same key value Entity Framework Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值 - Entity Framework Core : instance of entity type cannot be tracked because another instance with same key value EF:无法跟踪实体类型X的实例,因为已经跟踪了具有相同密钥的此类型的另一个实例 - EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked 实体框架核心 - 无法跟踪实体类型的实例,因为已在跟踪具有键值的另一个实例 - Entity framework Core - The instance of entity type cannot be tracked because another instance with the key value is already being tracked 实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例 - The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型 Model 的实例,因为已跟踪另一个具有相同 {&#39;Id&#39;} 键值的实例 - The instance of entity type Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked "使用 AsNoTracking() 获取“无法跟踪实体类型的实例,因为已经在跟踪具有相同键值的另一个实例”" - Getting 'The instance of entity type cannot be tracked because another instance with same key value for is already being tracked' with AsNoTracking() 无法跟踪实体类型“Bus”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例 - The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked 无法跟踪实体类型“AppUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM