作者:晨曦dora | 来源:互联网 | 2023-05-17 20:37
Whataretheadvantagesdisadvantagestoeachmethod?每种方法有哪些优点缺点?IknowIvereadsomewhereine
What are the advantages/disadvantages to each method?
每种方法有哪些优点/缺点?
I know I've read somewhere in either a book or on this site why using table inheritance is crappy for Entity Framework 4.
我知道我已经在书籍或本网站的某处阅读过为什么使用表继承对于Entity Framework 4来说很糟糕。
For instance, why not make one table which has an entityId, datecreated, datemodified and then have every other class inherit that in entity framework? Then my tables for all other entities don't need to have those columns. Then I can have a person class inherit that base class, and then a specific person inherit person.
例如,为什么不创建一个具有entityId,datecreated,datemodified的表,然后让其他所有类继承实体框架中的那个?然后我的所有其他实体的表不需要那些列。然后我可以让一个人类继承该基类,然后一个特定的人继承人。
I am not sure of the advantages of this though other than writing a smaller SQL script to generate the database...
除了编写一个较小的SQL脚本来生成数据库之外,我不确定这有什么优点......
Disadvantages I see are that it makes querying /viewing the data directly in SQL a big pain in the ass (all relevant information is broken across so many tables), and I also asked my friend who said:
我看到的缺点是,它直接在SQL中查询/查看数据是一个很大的痛苦(所有相关信息在这么多表中被打破),我也问过我的朋友说:
"The biggest thing that sticks out for me is the fact that i wouldn't want my database to rely on the current inheritance structure of my application. if, for whatever reason, i wanted to change the design of my application in terms of inheritance, it wouldn't matter because my data wouldn't be reliant on my current system design. i think of data storage as just that--storage. the database is normalized according to proper database design, but inheritance is a programatic choice of application development, not data storage. that alone would prevent me from using it. inheritance is a very difficult thing to do properly when it comes to designing an application. it's much easier to change application code than it is to change and migrate database data when most less-seasoned devs approach a problem they approach it with inheritance. i did too when i first started developing. it logically makes sense. however once developing for a long time you learn that delegation is really the best way to go (services calling services in the case of soa) and that single-purpose services provide a lot more reuse than inheritance."
Which also makes sense to me.
这对我来说也很有意义。
So
所以
1) In general, what are the pros/cons of inheritance vs extending
2) In my specific example above, what would be more appropriate?
3) If my example is crappy for either or both, what is a good example for using inheritance and for using extending?
1)一般来说,继承与扩展的利弊是什么2)在我上面的具体例子中,哪些更合适? 3)如果我的例子对于其中一个或两个都很糟糕,那么使用继承和使用扩展的好例子是什么?
I've used both before but as I am far from seasoned, I am still unsure how to handle all situations.
我之前使用过两者,但由于我远没有经验丰富,我仍然不确定如何处理所有情况。
10 Votes, 8 favorited, over a hundred views and no one can expand? =(.
10个投票,8个收藏,超过100个观点,没有人可以扩展? =(。
3 个解决方案
9
I'm a little late to the party, but I've been having the same questions as you regarding inheritance in EF and found a pretty good summary for you to read. It's an excerpt from Julie Lerman's book Programming Entity Framework.
我参加派对的时间有点晚了,但是我在EF的遗传方面遇到了同样的问题,并为你找了一个很好的总结。这是朱莉·勒曼(Julie Lerman)的“编程实体框架”(Programming Entity Framework)一书的摘录。
After reading it, here's my conclusions on the topic:
阅读之后,这是我对这个主题的结论:
1) In general, what are the pros/cons of inheritance vs extending - The pros really depend on the table strategy you choose. See How to choose an Inheritance Strategy - MSDN Blog. However, they are only "pros" on the assumption that you have already decided to use inheritance at all. And that is not a decision to take lightly.
1)一般来说,继承与扩展的优缺点是什么 - 优点实际上取决于您选择的表策略。请参见如何选择继承策略 - MSDN博客。但是,假设您已经决定使用继承,它们只是“专业人士”。这不是一个轻率的决定。
The cons are many, but the biggest is the inability to add an existing entity into the database as a derived entity. For example: I have a Student which inherits from Person. There is a Person record for John Smith. Some time later I need John Smith to be a Student. Well too bad. That's not possible. (At least not without circumventing EF with a stored procedure).
缺点很多,但最大的是无法将现有实体作为派生实体添加到数据库中。例如:我有一个继承自Person的学生。 John Smith有一个Person记录。一段时间后,我需要约翰史密斯成为一名学生。太糟糕了。那是不可能的。 (至少在没有通过存储过程规避EF的情况下)。
2) In my specific example above, what would be more appropriate? - In your example you should just add those columns (entityId, datecreated, datemodified) to tables that need them. You could use a Complex Type for datecreated and datemodified, but that wouldn't be necessary unless you're a very strict DRY guy. Even then, it might be overkill. The reason is that once you have an entity, you can never add that entity to another derived table. A Person (which is a BaseEntity) can not be added as a Student later. Also, writing LINQ queries would be far more complex than needed. Alex already showed that.
2)在我上面的具体例子中,哪个更合适? - 在您的示例中,您应该将这些列(entityId,datecreated,datemodified)添加到需要它们的表中。您可以使用复杂类型进行datecreated和datemodified,但除非您是一个非常严格的DRY家伙,否则这不是必需的。即便如此,它可能有点矫枉过正。原因是,一旦有了实体,就永远不能将该实体添加到另一个派生表中。一个人(这是一个BaseEntity)以后不能作为学生添加。另外,编写LINQ查询会比需要复杂得多。 Alex已经证明了这一点。
3) If my example is crappy for either or both, what is a good example for using inheritance and for using extending? - Generally, if you can make your base-type abstract, inheritance might work for you. But you should still consider other options first. If your base type will be directly instantiated somewhere, just use composition. Inheritance gets very troublesome when it comes to actually querying and inserting records.
3)如果我的例子对于其中一个或两个都很糟糕,那么使用继承和使用扩展的好例子是什么? - 通常,如果您可以创建基类型抽象,继承可能适合您。但你应该首先考虑其他选择。如果您的基本类型将直接在某处实例化,只需使用合成。在实际查询和插入记录时,继承变得非常麻烦。
In summary, just because you can do inheritance in EF does not mean you should. If you can get away with an inheritance-free design, then by all means do it.
总之,仅仅因为你可以在EF中进行继承并不意味着你应该这样做。如果你能摆脱无继承设计,那么一定要做到。