gpt4 book ai didi

c# - Entity Framework 5 和 TPT 设计问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:24 25 4
gpt4 key购买 nike

我首先编写代码并使用 Table-per-type 设计。当我将第二个对象扩展到多个表时出现以下错误:

实体或协会之间共享的值(value)在多个位置生成。检查映射是否不会将 EntityKey 拆分为多个存储生成的列。

我的数据库是这样的:

感谢投票,编辑添加我的图片: enter image description here

项目的 POCO 如下所示:

public abstract class Project {
public int ProjectID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<LocationElement> LocationElements { get; set; }


public abstract string getProjectIdentifier();

}

对于位置元素:

public enum HowObtainedCodes {
Provided = 1,
Estimated = 2,
Summarized = 3
}
public abstract class LocationElement {
public int LocationElementID { get; set; }
public int ProjectID { get; set; }
public HowObtainedCodes HowObtainedCodeID { get; set; }
}

还有一点:

[Table("ProvidedPoints")]
public class ProvidedPoint : LocationElement {
public double Lat { get; set; }
public double Long { get; set; }
public string Description { get; set; }
}

从项目(摘要)到科学许可的链接工作正常,我的对象按预期加载/保留。此外,如果我使它不抽象,我可以添加 LocationElements 对象。一旦我扩展 LocationElements 并尝试保存 ProvidedPoint 对象,我就会收到上述消息。我的第一个想法是将 ProvidedPoints 上的 LocationElementID 设置为标识列,但事实并非如此。

我的问题是:尝试以这种方式将两个 TPT 对象链接在一起时,我是否做了意想不到的事情?我还缺少其他东西吗?

最佳答案

正如上面@leppie 所指出的,我不得不用注释 [Table("LocationElements")] 装饰 LocationElement 类,这立即解决了问题。我对 EF 的理解是,对于 TPT 设计的基表,这不是必需的,而且我还没有在 Project/ScientificLicence 对上这样做(也就是说,我只装饰了 ScientificLicence 对象)。

我假设这与我保存新项目对象时添加/保留 LocationElements 的方式有关。如果有人有任何额外的见解,我很想知道更多。

希望这对其他人有帮助,非常感谢 leppie!

关于c# - Entity Framework 5 和 TPT 设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13754839/

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