【问题标题】:Class composition in Entity Framework 4Entity Framework 4 中的类组合
【发布时间】:2012-01-30 03:01:48
【问题描述】:

假设我有一个非常简单的类结构,更多的是为了方便。

class A{
    [Column("someValue")]
    public int someValue{get; set;}
}

[Table("tableB")]
class B{
    [Column("somethingElse")]
    public int somethingElse{get; set;}
    public A somethingEncapsulated{get; set;}
}

[Table("tableC")]
class C{
    [Column("otherSomething")]
    public int otherSomething{get; set;}
    public A somethingEncapsulated{get; set;}
}

而数据库结构,无法更改看起来如此

tableB
------
somethingElse int
someValue int

tableC
-----
otherSomething int
someValue int

是否可以在不消除A类的情况下在Entity Framework中表示这种结构?如果有,怎么做?

【问题讨论】:

  • without eliminating class A 是什么意思?
  • 其他替代方法是在类BC 中继承类A。然后你可以有几个映射选项,如Table per hierarchytable per typevincentlauzon.wordpress.com/2011/04/19/…
  • @Jayantha,继承不是我想到的方法。它可能有效,但首先想到的是说“B 是 A”是否恰当。如果 B 已经从另一个类、更合适的类继承怎么办?

标签: .net c#-4.0 entity-framework-4.1 ef-code-first


【解决方案1】:

您可以在这种情况下使用复杂类型映射。

[ComplextType]
public class A{
    [Column("someValue")]
    public int someValue{get; set;}
}

Go through this article

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2023-03-08
    • 2019-06-01
    相关资源
    最近更新 更多