2015-07-13 38 views
0

如何在EF中創建三重關係? 什麼我打算做的是這樣的:如何在第一個代碼中創建三重關係實體框架

Table Foo: (ID INT TEXT VARCHAR) 
Table Coo: (ID INT TEXT VARCHAR) 
Table Boo: (ID INT TEXT VARCHAR) 
Table FooCooBoo: (FKFoo INT FKCoo INT FKBoo INT) 

我預計,使用它下面的代碼可以實現:

public class Foo 
{ 
    public int Id { get; set; } 
    public string Text { get; set; } 

    public virtual ICollection<Boo> Boos { get; set; } 
    public virtual ICollection<Coo> Coos { get; set; } 
} 

public class Boo 
{ 
    public int Id { get; set; } 
    public string Text { get; set; } 

    public virtual ICollection<Foo> Foos { get; set; } 
    public virtual ICollection<Coo> Coos { get; set; } 
} 

public class Coo 
{ 
    public int Id { get; set; } 
    public string Text { get; set; } 

    public virtual ICollection<Boo> Boos { get; set; } 
    public virtual ICollection<Foo> Foos { get; set; } 
} 

但是輸出,每Icollection的EF創建一個簡單的許多桌子。 這可能嗎?使用EF創建三重關係?

回答

2

可能是:

public class FooCooBoo 
{ 
    [Key, ForeignKey("Foo")] 
    public int FooId { get; set; } 
    [Key, ForeignKey("Coo")] 
    public int CooId { get; set; } 
    [Key, ForeignKey("Boo")] 
    public int BooId { get; set; } 

    public virtual Foo { get; set; } 
    public virtual Boo { get; set; } 
    public virtual Coo { get; set; } 
} 

,那麼你必須在一些配置類設置的導航性能。

+0

我更喜歡上面的對象屬性的ForeignKey數據註釋...但這正是他想要 –

+0

你有2 Foo在這裏。錯字? –

+0

@kirsteng是ty。 – tschmit007

相關問題