Entity framework foreign key -
i have 2 classes
public class person { public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string email { get; set; } }
and
public class personwebsite { public int id { get; set; } public string website { get; set; } public int personid{ get; set; } }
i've seen stuff being done before
public class person { public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string email { get; set; } public icollection<personwebsite> personwebsites{ get; set; } }
how go implementing code when person initialized, personwebsites list automatically initialised , personwebsite objects have same personid class calls it.
lazy loading:
you can make personwebsites
property virtual:
public virtual icollection<personwebsite> personwebsites{ get; set; }
entity framework load database it's required. method requires have lazy loading enabled default:
dbcontext.contextoptions.lazyloadingenabled = true;
eager loading:
you can use include force entity framework load personwebsites
on first query:
dbset.include(p => p.personwebsites);
Comments
Post a Comment