[英]How can I add multiple navigation properties with the same Foreign Key in EF.Core 5?
I am trying to add a collection of new objects in EF.Core 5. These objects contain a one-to-many relationship expressed in a navigation property as well as a property that contains the FK object.我正在尝试在 EF.Core 5 中添加新对象的集合。这些对象包含在导航属性中表示的一对多关系以及包含 FK object 的属性。
Whenever I try and.AddRange(entities);每当我尝试 and.AddRange(entities); I get "The instance of entity type '{object}' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked."
我得到“无法跟踪实体类型‘{object}’的实例,因为已经在跟踪另一个与 {‘Id’} 键值相同的实例。”
How can I get ef.core to understand that the navigation property is identical and to only insert once?如何让 ef.core 了解导航属性是相同的并且只插入一次?
Also, I'm not yet worried about what is actually in the database, this fails when I call.AddRange(entities);另外,我还不担心数据库中的实际内容,当我调用时失败了。AddRange(entities); for the first time after constructing the context.
构建上下文后的第一次。
I did break this into a foreach loop and can see that it's the 2nd instance of the Holder
object when the exception is thrown, which tells me that EF.Core doesn't know how to handle a 2nd instance of the "same" object.我确实将它分解为一个 foreach 循环,并且可以看到它是
Holder
object 的第二个实例,当抛出异常时,这告诉我 EF.Core 不知道如何处理“相同”object 的第二个实例。
json json
[
{
"Id":637535121932347380,
"HolderId":100,
"Holder":{
"Id":100,
"Name":"Unit Test 0",
"Nickname":"Nickname 0"
}
},
{
"Id":637535121932363243,
"HolderId":100,
"Holder":{
"Id":100,
"Name":"Unit Test 0",
"Nickname":"Nickname 0"
}
},
{
"Id":637535121932363330,
"HolderId":100,
"Holder":{
"Id":100,
"Name":"Unit Test 0",
"Nickname":"Nickname 0"
}
}
]
Business logic商业逻辑
_context.Properties.AddRange(entities);
await _context.SaveChangesAsync(cancellationToken);
EF.Core Fluent Mapping EF.Core Fluent 映射
internal sealed class HolderMap : IEntityTypeConfiguration<Holder>
{
public void Configure(EntityTypeBuilder<Holder> builder)
{
builder.HasKey(p => p.Id);
builder.Property(p => p.Id).ValueGeneratedNever().IsRequired();
builder.Property(p => p.Name).IsRequired().HasMaxLength(80);
builder.Property(p => p.Nickname).HasMaxLength(96);
builder.HasMany(p => p.Properties).WithOne(p => p.Holder).HasForeignKey(h => h.HolderId);
}
}
internal sealed class PropertyMap : IEntityTypeConfiguration<Property>
{
public void Configure(EntityTypeBuilder<Property> builder)
{
builder.HasKey(p => p.Id);
builder.Property(p => p.Id).ValueGeneratedNever().IsRequired();
//relationships
builder.HasOne(p => p.Holder).WithMany(h => h.Properties).HasForeignKey(k => k.HolderId);
}
}
Right now I am getting the error message "When attaching existing entities, ensure that only one entity instance with a given key value is attached."现在我收到错误消息“附加现有实体时,请确保仅附加一个具有给定键值的实体实例。”
I wonder how you create entities from in the first place?我想知道您最初是如何创建实体的? If you have something like
如果你有类似的东西
MyEntity myEntity = new () {
Id = 637535121932347380,
Holder = new() {
Id = 10,
Name = "Unit Test 0"
}
}
then you don't need to explicitly assign HolderId
- Entity Framework will figure it by itself.那么您不需要显式分配
HolderId
- Entity Framework 将自行计算。 Because you are assigning, it probably thinks that there is another Holder
object with the same key因为你在分配,它可能认为有另一个
Holder
object 具有相同的密钥
UPDATE : if the first assignment works fine, and the second throws exception - even more curious how do you do the assignment?更新:如果第一次分配工作正常,第二次抛出异常 - 更好奇你是如何完成分配的? I assume you
ICollection<MyEntity>
on the one side of the relationship.我假设您
ICollection<MyEntity>
在关系的一侧。 What if you assign MyEntity
es to the collection instead?如果您将
MyEntity
分配给集合怎么办?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.