实体框架:模型优先,继承?
我有一个“人员”表、一个“雇员”表和一个“承包商”表。所有员工都是人,所有承包商都是人,每个人要么是员工,要么是承包商。像这样:
我如何使用模型优先来实现这个概念?遗产?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个“人员”表、一个“雇员”表和一个“承包商”表。所有员工都是人,所有承包商都是人,每个人要么是员工,要么是承包商。像这样:
我如何使用模型优先来实现这个概念?遗产?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您有三个选项:
1 - 每个层次结构表:
有利于性能,因为需要一张物理表。您需要向 Person 添加一个鉴别器字段 - 例如“PersonType”。这种方法的问题(我发现)是,您最终会得到很多可为空的字段,并且派生类型之间的导航属性很困难(根据我的经验)。
2 - 每种类型的表:
需要单独的表,但如果您想要另一个“人”类型,则有利于灵活性。
3 - Table-Per Concrete Type:没有这方面的经验,所以无法对此发表评论。 AFAIK 它与 TPT 非常相似。
我可能会选择 TPT,因为在我看来它更容易。
尽管如此,“Contractor”和“Employee”表中的字段具有相同的类型,因此您可以将其概括为 TPH 的单个字段。但我猜这不是完整的模型。
模型优先的步骤:
You've got three options:
1 - Table-Per Hierarchy:
Good for performance, as one physical table is required. You'll need to add a discriminator field to Person - such as "PersonType". Problem with this approach (what I have found), is you end up will lots of nullable fields, and navigational properties between derived types are difficult (in my experience).
2 - Table-Per Type:
Requires separate tables, but good for flexibility if you want to another another "Person" type.
3 - Table-Per Concrete Type: Don't have experience with this, so can't really comment on it. AFAIK it's very similar to TPT.
I'd probably go with TPT, just because it's easier IMO.
Having said that though, the field in the "Contractor" and "Employee" tables are of the same type, so you could generalize this as a single field with TPH. But i'm guessing that's not the complete model.
Steps for Model-First:
即表结构+继承==TPT。在设计器中,它看起来像这样:
...以及实体的原始 EDMX 和映射到这些表:
有关不同继承类型以及它们与物理数据库表的关系的更详细说明,请参阅:
http://huagati.blogspot.com/2010/ 10/mixing-inheritance-strategies-in-entity.html
...还有...
http://blogs.msdn.com/b/adonet/archive/2010/10/25/inheritance-mapping-a-walkthrough-guide-for-beginners.aspx
That table structure + inheritance == TPT. In the designer it will look something like this:
...and the raw EDMX for the entities and mappings to those tables:
For a more detailed description of the different inheritance types and how they relate to physical db tables, see:
http://huagati.blogspot.com/2010/10/mixing-inheritance-strategies-in-entity.html
...and...
http://blogs.msdn.com/b/adonet/archive/2010/10/25/inheritance-mapping-a-walkthrough-guide-for-beginners.aspx