简体   繁体   English

可以使用接口在 Entity Framework Codefirst 中创建我的模型吗?

[英]Can use interfaces in creating my models in Entity Framework Codefirst?

I'm new to Entity Framework and I'm practicing CodeFirst.我是实体框架的新手,我正在练习 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:我想要做的是创建由 Employee 继承的接口(IPersonalInfo、IContactInfo),使其看起来像这样:

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?如果没有,我该如何处理这种情况?

First of all, it sounds like you are confusing inheritance and composition.首先,听起来您混淆了继承和组合。 An example of inheritance would be having a common Person base class from which you could inherit an Employee or a Student.继承的一个例子是拥有一个公共的 Person 基类,您可以从中继承一个 Employee 或一个 Student。 An example of composition would be an Employee and a Supplier each composed with a common ContactInfo object, but having no common base class.组合的一个例子是一个 Employee 和一个供应商,每个都由一个公共 ContactInfo 对象组成,但没有公共基类。

When you design your entities, you are really designing the table structure of the underlying relational database.当您设计实体时,您实际上是在设计底层关系数据库的表结构。 You can model inheritance: a common base class can be represented by its own table and any common fields, and any specialized classes could be in their own table with a foreign key linking to the common table.您可以对继承进行建模:一个公共基类可以由它自己的表和任何公共字段表示,任何专门的类都可以在它们自己的表中,并带有一个链接到公共表的外键。 It may or may not make sense to do this - by breaking things up into separate tables, you're adding another join to your queries.这样做可能有意义也可能没有意义 - 通过将事物分解成单独的表,您正在向查询中添加另一个连接。 This will slow down performance.这会降低性能。

Composition can also be represented in a relational database, but it only really makes sense if it is a common component shared amongst many other data entities.组合也可以在关系数据库中表示,但只有当它是许多其他数据实体之间共享的公共组件时才真正有意义。 ContactInfo is almost always going to be unique for a given person/supplier, so it really doesn't make sense to break it out into a separate table - you're just adding an extra table join which, again, will slow down performance on your queries. ContactInfo 对于给定的人/供应商来说几乎总是唯一的,因此将其分解到单独的表中确实没有意义-您只是添加了一个额外的表连接,这再次会降低性能您的查询。

You should think about moving inheritance/composition up a layer in your design.您应该考虑在设计中将继承/组合向上移动一层。 The entities (data access layer) should match the relational database, but the domain model (ie business objects layer) should adhere to object-oriented principles.实体(数据访问层)应与关系数据库匹配,但域模型(即业务对象层)应遵循面向对象的原则。

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 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM