Entity Framework 4 Code-FirstCTP5で実際にどのように関係を実行するかについて簡単な例を示したいと思います。
これらの種類の関係の例が大好きです:
* one-to-many
* many-to-many
どうもありがとうございます!
Entity Framework 4 Code-FirstCTP5で実際にどのように関係を実行するかについて簡単な例を示したいと思います。
これらの種類の関係の例が大好きです:
* one-to-many
* many-to-many
どうもありがとうございます!
1対1
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;}
}
注意すべきこと、それは仮想でなければなりません
1対多
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;}
}
多対多
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;}
}
ICollectionである必要があることに注意してください
次のリンクが役立つかもしれません。クリックしてクリックしてください
お役に立てれば。
編集
1対多を含むように更新されました。
編集#2
コメントで要求された請求書<->製品シナリオを実行する可能性を含めるように更新されました。
注:これはテストされていませんが、正しい方向に進むはずです
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
}
これを使用して、請求書のすべてのアイテムを繰り返し処理し、foreachで各アイテムの請求書の詳細と製品自体の説明を表示できます。