实体框架:多重继承

发布于 2024-09-15 11:00:36 字数 746 浏览 1 评论 0原文

好吧,显然我在这里与 .net 作斗争……答案可能是我找错了树,应该继续使用我 5 年前的数据库设计。

我想要的是有一个抽象对象 Client 和 2 个继承对象Supplier 和 Customer。

客户与供应商和客户之间的关系是 1: 0/1

这似乎意味着基于继承的解决方案会很好地工作,因为它将使我能够编写更少的验证代码并利用强类型集合。

然而,开箱即用的实体框架坚持在客户端表上使用唯一的 id,并拒绝让您拥有从客户和供应商表引用的相同 id。

当您进入数据库并手动编辑此 id 使其在两个继承对象上相同时,您会遇到问题,因为客户端集合存在索引冲突(毫不奇怪)。

结果是我不能拥有一个同时显示为供应商和客户的客户(我对 2 个单独的对象共享相同 id 的想法感到满意)。

这导致了一个设计问题,即如何最好地模拟这种情况......我面临着必须为“isCustomer”、“isSupplier”或连接到客户端并写入的供应商和客户表进行条件连接和布尔字段的风险代码来与其保持0-1关系。

其他人一定也遇到过这样的问题吗?没有优雅的解决方案吗?

另一种选择: 我需要同一对象的多个视图。这意味着我不能简单地在一个字段上使用一个整数值来确定允许哪些视图。我需要使用几个布尔字段来确定允许哪些对象视图。

这在 EF 中可能吗?显然我可以在数据库中创建视图,但我宁愿从 EF 中创建视图并让它创建 SQL。 (虽然我已经定义了 sp 来在数据库中设置各种键和约束,但这意味着我越来越倾向于放弃使用 EF 生成我的架构)

Ok obviously I am fighting .net here ... and the answer is probably that i am barking up the wrong tree and should just get on with just using the kind of database design i would of 5 years ago.

What I want is to have an abstract object Client and have 2 inherited objects Supplier and Customer.

The relationship between client and both supplier and customer is 1: 0/1

This seems to mean that a solution based on inheritance would work well as it would enable me to write less validation code as well as lever the strongly typed collections.

However out of the box entity framework insists on using unique id's on the client table and refusing to let you have the same id referenced from the Customer and supplier tables.

when you go into the database and edit this id manually to be the same on both inherited objects you then get problems because the collection of clients has an index collision (unsuprisingly).

the result is that I cannot have a client who appears as both a supplier and a customer (I was happy with the idea of 2 seperate objects sharing the same id).

This leads into a design question of how to best model this scenario ... I am at risk of having to do a conditional join and boolean fields for 'isCustomer', 'isSupplier' or a suppliers and customers table that joins to client and write code to maintain the a 0-1 relationship with it.

Someone else must of had this kind of issue? No elegant solutions?

an alternative:
I need multiple views of the same object. This means that I cannot simply have an integer value on one field determining which views are allowed. I require using several bool fields to determine what object views are allowed.

Is this possible in EF? Obviously I can create views in the DB, but id rather do this from EF and have it create the SQL. (although i am already defining sp's to set up various keys and constraints in the db, but this means i am getting more and more likley to abandon using EF to generate my schema)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-09-22 11:00:36

不幸的是,你所要求的实际上是不可能的。 .NET 不允许多重继承,因此这不是(也不会是)EF 或任何其他 ORM 的功能。

您必须删除 EF 设计器中的继承,只允许表之间正常的 1:1* 关联。

您最终将得到您正在寻找的数据库设计,并且您的 C# 语法将如下所示:

Client c;

c.Customer.[...]
c.Supplier.[...]

Unfortunately, what you're asking for isn't really possible. .NET doesn't allow for multiple inheritance, so this isn't (and won't be) a feature of EF or any other ORM.

You'll have to remove the inheritance in the EF designer and just allow a normal 1:1* association between the tables.

You'll end up with the database design you're looking for, and your C# syntax will look like:

Client c;

c.Customer.[...]
c.Supplier.[...]
少年亿悲伤 2024-09-22 11:00:36

将客户和供应商共有的内容放在 client 中,仅将客户特有的内容放在 customer 中,将供应商特有的内容放在 supplier 中。

3 个表,3 种类型的对象。

Put whatever is common to customers and suppliers in client, and only the stuff specific to customers in customer, stuff specific to suppliers in supplier.

3 tables, 3 types of objects.

短叹 2024-09-22 11:00:36

我知道如果 5 岁的 EF 这篇文章仍然没有解决这个问题...

也许我错过了一些东西,但是 EF 不会让多个孩子从一个父母那里继承,而 DotNet 完全有能力做到这一点。我可以通过将每个实体更改为分部类,然后定义每个分部类实现的接口来解决这个问题。这太荒谬了。我们数据库中的每个表都有 4 列:CreatedBy、CreatedOn、UpdatedBy、UpdatedOn。如果我在实体框架中创建一个抽象类,我应该能够从这个抽象类继承每个实体,以便每个实体都有这四个字段。但你不能。抽象类只能继承一张表。这毫无意义。它完全违背了抽象类的目的。我希望每个实体都具有这四个字段,但只有一个孩子可以拥有它们。公司应该有这四个领域。 Facilities 是Corporation 的子项,也应该有这四个字段。但不行,做不到。尽管 TimeStampTable 是一个抽象类,但只有一个表可以继承,并且只有一个表获取四个字段

这是实体图

在此处输入图像描述

.....无论如何,我感受到你的痛苦,并且有一个解决方法。创建所需的接口,将实体更改为分部类,然后在一个单独的文件中,每个实体从该接口继承分部类,

您必须在每个分部类文件中手动实现该接口。

祝你好运(或者找到另一个 ORM)

I know this post if 5 years old by EF still hasn't fixed this...

Maybe I'm missing something, but EF won't let multiple children inherit from one parent and DotNet is perfectly capable of that. I can work around it by changing every entity to a partial class and then defining an interface that every partial class implements. It's ridiculous. Every single table in our database has 4 columns: CreatedBy, CreatedOn, UpdatedBy, UpdatedOn. If I create an abstract class in entity framework I should be able to inherit every entity from this one abstract class so that every entity has these four fields. But you can't. Only one table can inherit from an abstract class. It makes no sense. It defeats the purpose of an abstract class at all. I want every entity to come with these four fields but only one child can have them . Corporation should have these four fields. Facilities, a child of Corporation, should also have these four fields. But nope, can't do it. Even though TimeStampTable is an abstract class only one table can inherit and only one table gets the four fields

Here is the entity diagram

enter image description here

.....anyways, I feel your pain and there is a work around. create the interface you want, change your entities into partial classes, then in a separate file for every entity inherit the partial classes from that interface,

you have to implement that interface by hand in every partial class file.

Good luck (or find another ORM)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文