How do you actually execute relationships in Entity Framework 4 Code-First CTP 5?

I would like to give a brief example of how you actually perform relationships in Entity Framework 4 Code-First CTP 5?

I would like to admire an example of this kind of relationship:

* one-to-many * many-to-many 

Thanks a lot!

+7
source share
1 answer

One to one

 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;} } 

It should be noted that it must be virtual

One for many

 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;} } 

To many many

 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;} } 

Please note that this must be an ICollection

The following links may be useful, click and click

Hope this helps.

EDIT

Updated to include one of many.

EDIT No. 2

Updated to include the ability to run the Invoice ↔ Product script that was requested by the comment.

Note: this is not verified, but should put you in the right direction

 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 } 

Using this, you can iterate over all elements of the invoice, and then before specifying the invoice details about each element, as well as a description of the product itself.

+10
source

All Articles