实体框架:我可以从其他派生实体派生实体吗? [英] Entity framework: can I derive an entity from an other derived entity?

查看:112
本文介绍了实体框架:我可以从其他派生实体派生实体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在实体框架6.0中创建一个模型。



我有一个抽象的实体Person。

从这个抽象的实体我有一个派生类型的客户。



到目前为止常见的做法。



现在我想从客户实体派生一个实体,因为该模型适用于多家公司,每家公司都有针对其客户的特定项目。



模型看起来像



人物 - >客户 - > CompanyX_Cyustomer



我的问题是:

- 我可以从客户实体派生实体或

- 应该我将每个房产保留在客户实体中

- 如果我来自客户实体,我是否必须将客户实体设为抽象类型?



亲切的问候



Jeroen

Hello,

I'm creating a model in the entity framework 6.0.

I have an abstract entity Person.
From this abstract entity I have a derived type customer.

So far common practice.

Now i want to derive an entity from the customer entity, because the model is for multiple companies, each one having specific items for their customers.

The model looking like

Person -> Customer -> CompanyX_Cyustomer

My questions are:
- Can I derive an entity from the customer entity or
- should I keep every property in the customer entity
- If I derive from the customer entity, do I have to make the customer entity an abstract type?

Kind regards

Jeroen

推荐答案

根据我对你的问题的理解,我认为是的,这可以通过代码来完成,但不确定是否通过EDMX。这也是使用继承和减少冗余代码的最佳方式。



在C#中,你可以使用任何类作为Base,但是一个类可以有一个基础。所以你可以使用如下代码



As per my understanding to your question, I think yes, this can be done by code but not sure whether by EDMX or not. Also this is the best way of using inheritance, and reduce redundant code.

In C# you can use any class as Base, but a class can have a single base. So you can use as like below code

public class Person
{
   //Some properties & Methods for Person
}

public class Customer : Person
{
   //Some properties & Methods for Customer
}

public class CompanyX_Customer : Customer
{
   //Some properties & Methods for CompanyX_Customer
}



但是在创建构造函数时需要注意(默认情况除外)(如果有的话)。



希望它有用......


but you need to take care at the time of creating constructors (except default) if any.

Hope it may helpful...


我的问题是:

< b> - 我可以从客户实体派生实体或



是的,但您可以在edmx中手动编写designer.cs中的代码。



- 我应该保留客户实体中的每一处房产



当你继承子类成为基类的超级集时,你可以隐藏/覆盖属性但你不能删除任何属性。



- 如果我派生自客户实体,我是否必须将客户实体设为抽象类型?



并非总是如此,但这取决于您的方式如何为孩子提供服务这个。



如果你想提供一些基本的实现,你可以使用

1. 虚拟 [ ^ ]

2. 摘要 [ ^ ]



对你的一些帮助: http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework -code-first-ctp5-part-1-table-per-hierarchy-tph [ ^ ]

&

实体框架代码第一次继承:每个层次结构的表和每种类型的表 [ ^ ]

&

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity- framework-in-an-asp-net-mvc-application [ ^ ]
My questions are:
- Can I derive an entity from the customer entity or

Yes you can but in edmx you need to manually write the code in designer.cs.

- should I keep every property in the customer entity

When you inherit the child class became the supper set of base class you can hide/override property but you cannot remove any property.

- If I derive from the customer entity, do I have to make the customer entity an abstract type?

Not always but it depends on your approach how you want to serve child class.

If you want to provide some basic implementation then you can use
1. virtual [^]
2. Abstract [^]

Some help for You : http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph[^]
&
Entity Framework Code First Inheritance : Table Per Hierarchy and Table Per Type[^]
&
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application[^]


这篇关于实体框架:我可以从其他派生实体派生实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆