c# - Entity Framework Inheritance - Multiple child instances of same base instance -
consider following model:
public partial class person { public int id { get; set; } public string name { get; set; } } public partial class teacher : person { public string classname { get; set; } } public partial class student : person { public int numberofclasses { get; set; } } using model , entity framework, possible have "student" instance , "teacher" instance both derived same base "person" instance? in other words, can "person" both "student" , "teacher"?
if so, best inheritance strategy use represent scenario?
the "abstract" keyword key this. not sure why using "partial". need select strategy first - link explains all. http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application
public abstract class person { public int id { get; set; } public string name { get; set; } } public class teacher : person { public string classname { get; set; } } public class student : person { public int numberofclasses { get; set; } } *edit - example useage *
public void getsomedetailaboutaperson(person person) { return person.someshareddetailfrombaseclass; } public void something() { teacher teacher = myservice.getteacherbyid(3); var somedetailorother = this.getsomedetailaboutaperson(teacher); }
Comments
Post a Comment