gpt4 book ai didi

c# - EF DB-first 映射困惑

转载 作者:太空狗 更新时间:2023-10-29 23:32:43 25 4
gpt4 key购买 nike

由于 EF 的这个小怪癖,我遇到了令人沮丧的情况。这是一个简单的行为演示。首先是数据库架构:

enter image description here

如您所见,RestrictedProduct 是产品的特例,我打算使用一些特殊代码将其作为 Product 的子类。

现在我导入到 EF 数据模型:

enter image description here

糟糕! EF 看到 RestrictedProduct 只有 2 个字段,都是 FK,因此它将其映射为 ProductRestriction 之间的一对多关系。因此,我返回数据库并将 Dummy 字段添加到 RestrictedProduct,现在我的 EF 模型看起来好多了:

enter image description here

但是 Dummy 字段是愚蠢且毫无意义的。也许我可以删除它?我从数据库表和实体模型中吹走了字段,然后从数据库中刷新模型...

enter image description here

哦,不! Product-Restriction 关联以新名称 (RestrictedProduct1) 回归!另外,它不会编译:

Error 3034: Problem in mapping fragments starting at lines (x, y) :Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.

除了在 RestrictedProduct 表中保留 Dummy 字段之外,是否有任何方法可以防止这种行为?

最佳答案

我刚遇到同样的问题,作为将虚拟字段放入 RestrictedProduct 表中以强制创建实体的替代方法,您还可以制作 RestrictedProduct.RestrictionId 字段可为空,然后 EF 将为它生成一个实体。然后您可以修改它以使用继承,并且任何后续的“从数据库更新模型”都不会导致不需要的导航属性。这不是一个很好的解决方案,而是一种解决方法。

关于c# - EF DB-first 映射困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14600252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com