IT:AD:EF/CodeFirst:HowTo:Define Models/Relationships/Examples/1-n
Process
//1-n
modelBuilder.Entity<Invoice>()
.HasMany(bcwi => bcwi.LineItems)
.WithRequired()
.HasForeignKey(bc => bc.InvoiceFK);
Working with:
//Requirements to enable the use of change-tracking POCO Proxies:
//* Class must be public, non-abstract, non-sealed.
//* Marking (all) scalar properties as virtual makes the class able to be proxied (more efficient)
//* Public *virtual* get/set for all persisted properties.
//* Collection based Navigation properties must implement ICollection<T>
// Ref: http://bit.ly/KgJPcU
//* Collections marked virtual are lazy loaded
public class Invoice
{
public virtual int Id { get; private set; }
...
//Marking a *Navigation Collection* as virtual, marks it for Lazy Loading
public virtual ICollection<LineItem> LineItems {
get { return _lineitems??(_lineitems = new Collection<LineItem>()); }
private set { _lineitems = value; }
}
private ICollection<LineItem> _lineitems;
}