【问题标题】:Entity Framework 4.1, Table Per Type Inheritance and Lookup TablesEntity Framework 4.1,每个类型继承表和查找表
【发布时间】:2011-06-10 15:24:52
【问题描述】:

使用每个类型继承的表,是否可以在多个派生类型中拥有具有相同键的相同数据?

例如

基本类型:

[Table("BaseType")]
public abstract class BaseType
{
    public int Id { get; set; }
    public string Description { get; set; }
}

派生类型A:

[Table("DerivedTypeA")]    
public class DerivedTypeA : BaseType {}

派生类型 B:

[Table("DerivedTypeB")]
public class DerivedTypeB : BaseType {}

除了DerivedTypeADerivedTypeB 的多种类型之外,我还有一种特定类型,即DerivedTypeADerivedTypeB

当我通过为此类型创建新的DerivedTypeADerivedTypeB 来为数据库播种时,它会被添加两次,不同的Ids。虽然不是世界末日,但我希望它在两个派生表中具有相同的 Id

这可能吗?

提前致谢。

【问题讨论】:

    标签: entity-framework ef-code-first table-per-type


    【解决方案1】:

    如果我的理解正确,您描述的是 C# 不支持的多继承情况。 DerivedA 不能是 DerivedB,反之亦然,因为它们是兄弟姐妹。

    本文有助于定义问题并提出一种可能对您有所帮助的模式: http://www.codeproject.com/KB/architecture/smip.aspx

    【讨论】:

    • 感谢您的快速回答。我不确定这是多继承的情况 - DerivedA 和 DerivedB 仍然只继承一个抽象类 BaseType 而不是多个类。
    • 是的,但是您说您有一种类型,即both DerivedA 和 DerivedB。在 C# 中实现这一点的唯一方法是,如果 DerivedB 派生自 DerivedA(或相反),而不是两者都派生自 BaseType。
    • 我接受你的多重继承点。最后,我设法重组了我的数据,以便所有类型都是 DerivedA 或 DerivedB,并且现在都按预期工作。谢谢。
    猜你喜欢
    • 2012-11-30
    • 2013-06-13
    • 2020-06-08
    • 2011-08-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多