database - Why do Entity Framework classes need a virtual member of an unrelated class -


easier show example -- i'm using code-first construct database. have following classes:

public class blog     {         public int id { get; set; }         public string title { get; set; }         public string authorname { get; set; }         public list<post> posts { get; set; }         public string blogcode         {                         {                 return title.substring(0, 1) + ":" + authorname.substring(0, 1);             }         }     }      public class post     {         public int id { get; set; }         public string title { get; set; }         public string content { get; set; }         public virtual blog blog { get; set; }     } 

i don't understand why post needs public virtual blog blog. act foreign key in database link blog? seems if case use blog id.

it allow 2 tables related , places foreign key on post relating blog.

having public virtual blog blog { get; set; } allows reference blog object post object , access properties of blog. e.g. mypost.blog.id if used public virtual int blogid { get; set; }, not able since blogid int value.

if domain objects lazy loaded, mypost.blog not hydrated data database (i.e. no call blog table) until property used. used, entity framework make database call , hydrate object data blog table. part of beauty of using orm... allows focus on code while takes care of database operations.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -