答えて

10

一つ

public class One 
{ 
    public int Id {get;set;} 
    public virtual Two RelationTwo {get;set;} 
} 

public class Two 
{ 
public int Id {get;set;} 
public virtual One RelationOne {get;set;} 
} 

一つの物事、それは多くの多く

に多く

public class One 
{ 
    public int Id {get;set;} 
    public virtual ICollection<Two> RelationTwo {get;set;} 
} 

public class Two 
{ 
public int Id {get;set;} 
public virtual One RelationOne {get;set;} 
} 

に仮想

一つである必要がありますそれは多分役に立つリンクに続いているICollection

にする必要があることを

public class One 
{ 
    public int Id {get;set;} 
    public virtual ICollection<Two> RelationTwo {get;set;} 
} 

public class Two 
{ 
public int Id {get;set;} 
public virtual ICollection<One> RelationOne {get;set;} 
} 

ノート、このことができますclickclick

希望。多くの1つを含むように更新

EDIT

。コメントによって要求された>製品シナリオ - 請求書<を行うための可能性を含むように更新

EDIT#2

注:これはテストされていないが、あなたはその後、請求書にすべての項目を反復処理し、その後、foreachの請求書の詳細を表示することができる可能性があり、これを使用して、右方向に

public class Invoice 
{ 
    public int Id {get;set;} 
    //.. etc. other details on invoice, linking to shipping address etc. 

    public virtual ICollection<InvoiceProduct> Items {get;set;} 
} 

public class InvoiceProduct 
{ 
    public int Id {get;set;} 
    public int Quantity {get;set;} 
    public decimal Price {get;set;} // possibly calculated 
    //.. other details such as discounts maybe 

    public virtual Product Product {get;set;} 
    public virtual Invoice Order {get;set;} // maybe but not required 
} 

public class Product 
{ 
    public int Id {get;set;} 
    //.. other details about product 
} 

あなたを置く必要があります各項目についての情報と、製品自体の説明が含まれています。

+2

私は彼らが仮想である必要があるとは思わない(しかし、私は彼らを仮想にすることを強く勧めます)。仮想としてマークされていない場合、関係はまだ存在しますが、EFは遅延読み込みを使用せず、対応するエンティティがセッションにロードされている場合にのみ関係のもう一方の側を読み込みます。また、1対多の場合、どのようにそれを行うかはあなたの答えによって推測することができますが、アプリケーションのニーズを犠牲にしても意味があるかもしれませんが、両側での参照は必要ありません。 –

+1

あなたが関心を持つもう一つのリンクは、最初にコードを書くのにはかなり便利なイントロです。http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with -entity-framework-4.aspx – Manatherin

+0

これまでの回答に感謝します。もう一つ。あなたの請求書が明細行を持っている場合は、それをBillコンテキストで使用します。これには関係テーブルが必要です。このシナリオをどのように進めますか? – Rushino

関連する問題