【发布时间】:2021-12-07 13:54:16
【问题描述】:
我没有找到任何有关此问题的资源,可能是措辞错误的原因。
设置是一个实体Lake,其中包含Fish 的列表。每个Fish 实体还包含一个其类型的实体FishKind。通过一些 API,我得到了一个 Lake 已经包含很多 Fish 的对象。由于某种原因,存在相同种类的鱼 -> 因此我们有多个具有相同 ID 的 FishKind 类的对象/实例。
这里是一些关于 Object Hiracy 的虚拟代码:
class Lake {
public int LakeId;
public List<Fish> Fish;
}
class Fish {
public int FishId;
public FishKind Kind;
}
class FishKind {
public int FishKindId;
FishKind(int id) { FishKindId = id; }
}
// this is how example data from the API could look like - I don't create objects like this in my code. I get them via the API
// The FishKind have the same id/data however a different instances in C#
// The FishKind have valid IDs which are also already in the DB
// only Lake and Fish should be created
var lake = new Lake();
var idOfShark = 29;
lake.Fish.Add(new Fish("Tony", new FishKind(idOfShark)));
lake.Fish.Add(new Fish("Helen", new FishKind(idOfShark)));
进一步说明 - 通过 API 接收的示例数据
{
"LakeId":0,
"Fish":[
{
"FishId":0,
"Name":"Tony",
"Kind":{
"FishKindId":29
}
},
{
"FishId":0,
"Name":"Helen",
"Kind":{
"FishKindId":29
}
}
]
}
- 当我执行
db.AddAsync(Lake)时,它会失败,因为我已经将这种鱼添加到数据库中 - 当我尝试
db.Update(Lake)时,只要我没有两条同种鱼,它就可以工作。如果他们是同一种我得到:The instance of entity type 'FishKind' cannot be tracked because another instance with the key value '{FishKindId: 1}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.'
我想到的一种解决方法是将所有 FishKind 实例替换为已跟踪的对象,但这似乎很复杂,并且像一个 hacky 解决方案。
【问题讨论】:
-
@T.Trassoudaine 我只写了一些最少的代码。确保所有对象都有 ID,否则我无法创建迁移。我编辑了帖子以添加 ID
-
@T.Trassoudaine 我不想创建新的 FishKind。从另一个湖的创建中,这些种类可能已经存在。 (或者在创建新的鱼/湖时在 GUI 中导致我从数据库加载所有种类)
-
@T.Trassoudaine 我不想创建新种类。但是我通过 Rest 获取对象,所以它们不同。上述解决方法是查找所有 FishKinds 并用跟踪的 obj 替换它们
-
从 API 获取数据的实际代码怎么样,知道为什么要为每个 Fish 创建一个新的 FishKind 实例吗?
-
@T.Trassoudaine 我认为这是因为 asp.net 不知道对象是相同的实体,因此创建相同的对象但不同的实例
标签: c# asp.net-core entity-framework-core .net-6.0