实体框架4 TPH继承,如何改变一种类型到另一个? [英] Entity Framework 4 TPH inheritance, how to change one type into another?

查看:149
本文介绍了实体框架4 TPH继承,如何改变一种类型到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现关于这个,但还不足以让我明白什么最好的做法是为这种情况的一些信息。我有你的typicaly TPH与设置的抽象基类企业。我有几个孩子小公司,大公司等企业从继承。在现实中我其实对企业不同的现实,但分类我试图保持简单在这个例子中。在数据库按TPH我有所有这些类型的区分一个FirmTypeId列(int)的单个企业表。一切都很正常,除了我有一个要求,允许用户改变一个类型的公司到另一个。例如,一个用户可能已经取得加入该公司时,想将其从大公司变成小公司的错误。由于实体框架不允许暴露鉴别数据库列公开为一个属性,我不相信有一种方法,通过EF把一种类型到另一个。如果我错了,请大家指正。我看到它的方式,我有两个选择:

I have found some information regarding this but not enough for me to understand what the best practice for is for this scenario. I have your typicaly TPH setup with an abstract base class "Firm". I have several children "Small Firm", "Big Firm" etc inheriting from Firm. In reality I actually have different realistic classifications for firms but I am trying to keep it simple in this example. In the database as per TPH I have a single Firm table with a FirmTypeId column (int) that differentiates between all these types. Everything works great except I have a requirement to allow a user to change one type of firm into another. For example a user might have made a mistake when adding the firm, and would like to change it from Big Firm to Small Firm. Because entity framework does not allow exposing the discriminating database column to be exposed as a property, I don't believe there is a way to change one type into another via EF. Please correct me if I am wrong. The way I see it I have two options:


  1. 请不要使用TPH。只要有一个坚定的实体,回去使用。凡(FirmTypeId ==东西)的类型来区分。

  2. 执行SQL直接使用context.ExecuteStoreCommand更新数据库的FirmTypeId列。

我看到一个帖子,人们认为一个面向对象的原则就是实例不能改变它们的类型。虽然这非常有意义,我似乎就是不能够连接点。如果我们遵循这个规则,那么唯一一次使用任何类型的继承(TPH / TPT)是当一个人相信一种永远不会被转换成另一种。所以,小企业永远不会成为一家大公司。我看到成分应该使用的建议。即使它没有任何意义,我(意思是我没有看到一个公司如何有一家大公司,给我一个大公司是一个坚定的),我可以看到组合物可在EF如果数据是在建模多个表。然而,在一个情况下,我有一个单一的数据库中的表似乎是TPH或我在#1和#以上2所描述的东西。

I've seen a post where people suggest that One of the tenets of OOP is that instances cannot change their type. Although that makes perfect sense to me, I just don't seem to be able to connect the dots. If we were to follow this rule, then the only time to use any kind of inheritance (TPH/TPT) is when one is sure that one type would never be converted into another. So a Small Firm will never become a Big Firm. I see suggestions that composition should be used instead. Even though it doesn't make sense to me (meaning I don't see how a Firm has a Big Firm, to me a Big Firm is a Firm), I can see how composition can be modeled in EF if the data is in multiple tables. However in a situation where I have a single table in the database it seems it's TPH or what I've described in #1 and #2 above.

推荐答案

是的,你说这一切的权利。 EF继承不支持此方案。最好的方式来改变的公司的类型为现有的公司的是使用存储过程。

Yes, you got it all right. EF inheritance does not support this scenario. The best way to change a Firm type for an existing Firm is to use a stored procedure.

请看看这篇文章的详细信息:
结果
<一href=\"http://stackoverflow.com/questions/3823662/changing-inherited-types-in-entity-framework/3823925#3823925\">Changing在实体框架

Please take a look at this post for more info:
Changing Inherited Types in Entity Framework

这篇关于实体框架4 TPH继承,如何改变一种类型到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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