7
Entity Framework 4 Code-First CTP 5で実際にリレーションシップをどのように実践しているかの短い例がありますか?Entity Framework 4 Code-First CTP 5で実際にどのようにリレーションシップを実行しますか?
* one-to-many
* many-to-many
おかげでたくさん:
は関係のこれらの種類の例を大好きです!注意すべき
Entity Framework 4 Code-First CTP 5で実際にリレーションシップをどのように実践しているかの短い例がありますか?Entity Framework 4 Code-First CTP 5で実際にどのようにリレーションシップを実行しますか?
* one-to-many
* many-to-many
おかげでたくさん:
は関係のこれらの種類の例を大好きです!注意すべき
一つ
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;}
}
希望。多くの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
}
あなたを置く必要があります各項目についての情報と、製品自体の説明が含まれています。
私は彼らが仮想である必要があるとは思わない(しかし、私は彼らを仮想にすることを強く勧めます)。仮想としてマークされていない場合、関係はまだ存在しますが、EFは遅延読み込みを使用せず、対応するエンティティがセッションにロードされている場合にのみ関係のもう一方の側を読み込みます。また、1対多の場合、どのようにそれを行うかはあなたの答えによって推測することができますが、アプリケーションのニーズを犠牲にしても意味があるかもしれませんが、両側での参照は必要ありません。 –
あなたが関心を持つもう一つのリンクは、最初にコードを書くのにはかなり便利なイントロです。http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with -entity-framework-4.aspx – Manatherin
これまでの回答に感謝します。もう一つ。あなたの請求書が明細行を持っている場合は、それをBillコンテキストで使用します。これには関係テーブルが必要です。このシナリオをどのように進めますか? – Rushino