需要实体框架每种类型的表建议

发布于 2024-11-09 14:46:29 字数 1125 浏览 0 评论 0原文


我需要实现具有 1 个基类和 3 个子类(4 个类)的解决方案
基类: 用户
子类: ClientOfficeUserEmployee

在我的数据库中,我只有 3 个表:用户客户员工
我没有 OfficeUsers 表,因为我需要的所有数据都已在 Users 表中。
将来我希望能够创建列出客户、员工和办公室用户数量的报告。

我不想使用 TPH,因为“客户”和“员工”表中有很多不可为空的字段。
我是否应该创建仅包含 UserId 的 OfficeUsers 表,以便我可以实施 TPT?
对我来说,它看起来不是很好的设计——只有 PK 的表,这样我就可以正确地映射它——如果这是这样做的方法,请纠正我。

另一种选择是在 Users 表中添加 UserType 列并将其用作鉴别器,但它可以与 TPT 一起使用吗?是否可以创建缺少 1 个表的 TPT 并使用鉴别器,看起来像是混合 TPT 和 TPH,我认为这是不可能的。

预先感谢您的回答。

编辑:

另请考虑这种情况:
我引入了名为 MobileUser 的新类,它也具有与 User 相同的字段。在这种情况下,如果不引入新的用户类型列,我就无法知道系统有多少 MobileUsers 和多少 OfficeUsers
在这种情况下,有 2 个空表(仅 PK)比在我的查询中创建对表数量的依赖更好/更差,并且还阻止我使用一些 LINQ 查询(请参阅 Ladislav Mrnka 答案下的评论)

编辑 2 :

将来我有可能必须向 OfficeUser 添加字段,因此我开始认为空表可以以某种方式作为一种选择,至少是 C# 代码(查询)会看起来更干净。如果您有更好的方法,请告诉我。

I need to implement solution with 1 base class and 3 sub-classes (4 classes)
Base class: User
Sub-classes: Client, OfficeUser, Employee

In my database I have only 3 tables : Users, Clients and Employees.
I don't have OfficeUsers table since all data that I need is already in Users table.
In the future I want to be able to create report lisitng number of Clients, Employees and OfficeUsers.

I don't want to use TPH since I have lots of non-nullable fields in Clients and Employees table.
Should I create OfficeUsers table with only UserId so I can implement TPT?
It looks for me as not very good design - having table with only PK so I can map it properly - please correct me if this is the way to do it.

Another option was to have UserType colum within Users table and use it as discriminator but will it work with TPT? Is it possible to create TPT with 1 missing table and use discriminators, looks like mixing TPT and TPH which I think is not possible.

Thanks in advance for your answers.

EDIT:

Please also consider this scenario:
I'm introducing new class called MobileUser which also has the same fields as User. In that case I have no way of knowing how many MobileUsers and how many OfficeUsers is the system without introducing new column for user type.
Is having in this scenario 2 empty tables (only PK) is better/worse than creating dependency in my queries on number of tables and additionally preventing me from using some LINQ queries (please see my comments under Ladislav Mrnka answer )

EDIT 2:

There is a chance that I'll have to add fields to OfficeUser in the future so I'm starting to think that empty table can somehow be an option, at least C# code (queries) would look cleaner. Let me know if you have better approach.

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

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

发布评论

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

评论(2

ま柒月 2024-11-16 14:46:29

我认为这只是一个视角问题,而不是一个架构问题……因为无论你做什么,你最终都会得到一个 PK 表。

您可以创建一个OfficeUsers表,其中可以仅包含用户的PK...只是不要将其设为继承类型。现在您有了使用该办公室的所有用户的列表,您可以对其进行查询。结构是一模一样的,只是思路有点不同。

如果您有多个办公室,您将有一个带有 id 的办公室表,那么您的 OfficeUser 将拥有自己的类型表,因为额外的字段将是办公室外键...给您您想要的区别。

但是,由于您(我认为)只有一个办公室,因此不需要外键,因此您只需要一张表来保存使用该办公室的用户......这是“一个的 6 个,一个的六个”其他”,实际上完全相同,无论您选择哪种方式。

这就是为什么我会在第二次编辑中遵循您的直觉,您稍后可能会添加更多字段,因此您也可以创建一个“空”类型......因为无论哪种方式,您最终都会得到一个表格存储 PK。

I think this is just a problem of perspective, rather than an architectural one... because whatever you do, you end up with a single PK table.

You can make an OfficeUsers table, which can contain just the PK of the User... just don't make it an inherited type. Now you have a list of all users who use the office, which you can query against. The structure is exactly the same, but the thinking is a bit different.

If you had multiple offices, you'd have a table of offices with an id, then your OfficeUser would have it's own type table, as the extra field would be the office foreign key... giving you the distinction you wanted.

But, as you only (I presume) have one office, you don't need a foreign key, so you just need one table to hold the users who use the office... it's "6 of one, half a dozen of the other", exactly the same really, whichever way you choose.

This is why I'd go with your instinct in your 2nd edit, you might add more fields later, so you may as well make an "empty" type... because either way, you'll end up with a table that just stores PKs.

定格我的天空 2024-11-16 14:46:29

如果您的 OfficeUserUser 完全相同,那么您不需要任何额外的类。使用 User 而不是 OfficeUser 以及 EmployeeClient 的派生类

If your OfficeUser is exactly same like User then you don't need any additional class. Use User instead of OfficeUser and derived classes for Employee and Client

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