实体框架 - 继承 [英] Entity Framework - Inheritance

查看:47
本文介绍了实体框架 - 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我不记得这是否可能,但我确信它应该是。

我的数据库中有一个表,如下所示:

Hi All,

I can't remember if this is possible, but I'm sure it should be.
I have a table in my database which looks as follows:

Id [bigint] [primary key + auto incremented]
Name [nvarchar(50)]
UserType [tinyint]
LineManagerId [bigint] [nullable]
DivisionHeadId [bigint] [nullable]



有一个关系限制LineManagerId和DivisionHeadId是同一个表中的有效ID。



我'你肯定可以从那张图片中得到结构吗?如果没有问:-)

我正在使用Entity Framework来创建我的对象,它会带来User表没有问题。



我想要做的是拥有以下物品:


There is a relationship limiting LineManagerId and DivisionHeadId to be valid ID's from the same table.

I'm sure you can from that picture the structure? If not ask :-)
I'm using Entity Framework to create my objects and it brings in the User table with no problem.

What I want to do is have the following objects:

public class User
public class LineManager : User
public class DivisionHead : User



这些应该所有都映射到同一个表,使用UserType字段作为区分因素。



用户应该有0..1直线经理和0..1 DivisionHead的

LineManagers可以拥有多个用户和0..1 DivisionHead,并且该类应该有一个用户导航属性。

DivisionHeads可以有很多用户,而且该类应该有一个用户导航属性。



我已经能够制作来自我的Users实体的派生类,但是当我尝试构建它时,我得到错误 UserType没有默认值且不可为空但是,我已经设置了默认值UserType为1.



(我试图将此值转换为枚举,但会导致以下错误:允许使用默认值仅适用于非空间原始类型



所以我的问题是,我怎样才能做到这一点?或者更准确地说我错过了什么,因为我有课程我不能让它们起作用?

我会认为这很简单,我觉得我很接近,但到目前为止我的研究还没有对这个问题没有任何启发。我希望你们可以吗?



谢谢,


These should all map to the same table using the UserType field as the differentiating factor.

Users should have 0..1 Line Manager's and 0..1 DivisionHead's
LineManagers can have many users and 0..1 DivisionHead's, and the class should have a "users" navigation property.
DivisionHeads can have many users and the class should have a "users" navigation property.

I've been able to make a derived class from my Users entity, however when I try to build it I get the error UserType has no default value and is not nullable yet, I've set the default value of UserType to be 1.

(I tried to convert this value to an enum but that results in the following error: default values are allowed only for non-spatial primitive types)

So my question is, how can I achieve this? Or more accurately what am I missing as I have the classes I just can't make them work?
I would have thought it would be straightforward and I feel I'm close but my research so far isn't shedding any light on the issue. I'm hoping you you guys can?

Thanks,

推荐答案

这是一个有用的博客系列:

http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph



这个博客系列中描述了不同的继承方法。



现在你是什么使用UserType字段是有问题的,因为EF将自动尝试为派生类型执行一些关系映射。我不确定为什么它会与你试图实施的机制发生冲突,但后来我们还没有看到那些代码。



我发现的最佳路线是使用TPT模型(流畅映射派生类ToTable()或属性[Table()])并让EF处理引擎盖下的关系布线。



这些方法旨在使用代码优先实体框架方法,并且所描述的机制将无法按照指定的方式工作一个.edmx驱动的DAL。您可以调整一些数据库,将结果表推断为可行的系统;您可以为TPH做的一件事是将派生类的属性的无空列添加到表中以及类型字段(CLR类型)。这应该超过驼峰,但首先使用代码构建测试TPH应该为您提供TPH模型的确切构造,并且您可以从那里推断,以防我错过任何细节。
There's a helpful blog series at:
http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

The different inheritance approaches are well-described in this blog series.

Now what you're doing with the UserType field is problematic, as EF will automatically try to do some relational mapping for derived types. I'm not sure why it would conflict with the mechanism that you've tried to put in place, but then we haven't seen that code.

The best route that I've found is to use the TPT model (fluent map a derived class ToTable() or attribute [Table()]) and let EF handle the wiring of the relationship under the hood.

These approaches are designed to work with Code-First Entity Framework approaches, and the described mechanisms will not work as specified with an .edmx driven DAL. You can tweak the database some to extrapolate the resulting tables into a workable system; one thing that you can do for TPH is to add null-able columns for the properties of the derived classes into the table as well as a type field (CLR type that is). That should get it over the hump, but building a test TPH using code first should give you the exact construction of a TPH model, and you can extrapolate from there, in case I missed any details.


这篇关于实体框架 - 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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