I’m new to Entity Framework and I’m practicing CodeFirst. My problem is I’m creating a model class and I want that class to inherit from two other classes. For example, an Employee has personal information such as first name,middle name,last name, and etc… it has also a contact information such as address,phone,email, and etc… Students also has those properties as well. The reason why I separated those information into two different classes was that, another entity also can have contact information but without personal information, such as a company,schools,hospitals,warehouses and etc…
Sample codes:
public class ContactInfo
{
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
public class PersonalInfo
{
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
}
public class Employee : // This should be inheriting from PersonalInfo and ContactInfo
{
public int EmployeeID { get; set; }
}
public class Supplier : ContactInfo // Inheriting from ContactInfo and no PersonalInfo
{
public int SupplierID { get; set; }
public string Name { get; set; }
}
What I wanted to do is to create interfaces (IPersonalInfo,IContactInfo) to be inherited by Employee so that it will look like this:
public class Employee : IPersonalInfo,IContactInfo
{
public int EmployeeID { get; set; }
}
Is this a good practice? And if not, how can I manage with this kind of scenario?
Ys, You have to define the inheritance in the model. After that make sure you define one/many to one/many relationship. Refer link here, good tutorial.
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application