it:ad:code_first:howto:define_models:relationships:examples:1-n

IT:AD:EF/CodeFirst:HowTo:Define Models/Relationships/Examples/1-n

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

  • /home/skysigal/public_html/data/pages/it/ad/code_first/howto/define_models/relationships/examples/1-n.txt
  • Last modified: 2023/11/04 03:03
  • by 127.0.0.1