2012-01-30 81 views
0

假設我有一個非常簡單的類結構,爲了方便起見,我比其他任何東西都更方便。實體框架中的類構成4

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類?如果是這樣,怎麼樣?

+0

你是什麼意思'沒有消除類A'? – 2012-01-30 03:52:26

+1

其他的替代方法是繼承'B'和'C'類的'A'類。然後你可以有幾個映射選項,比如'Table per hierarchy','table per type'。 http://vincentlauzon.wordpress.com/2011/04/19/entity-framework-4-1-inheritance-7/ – 2012-01-30 04:02:52

+0

@Jayantha,繼承不是我想到的方法。它可能有效,但首先要考慮的關注點是說「B是A」的恰當性。如果B已經從另一個班級繼承,那麼更合適的班級呢? – MushinNoShin 2012-01-30 05:13:15

回答

2

在這種情況下,您可以使用複雜類型映射。

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

Go through this article