简体   繁体   English

选择正确的实体关系方法

[英]Choosing the right entity relations approach

Suppose I have a user table and role table 假设我有一个用户表和角色表

I have two role types, employee and employer 我有两种角色类型,员工和雇主

Inside user table, i have the following columns 在用户表中,我有以下几列

  • UserId 用户身份

    FirstName 名字

    LastName

    RoleId(foreign key -- one to many) RoleId(外键-一对多)

An employer can have related companies while employee can have related CVs 雇主可以拥有相关的公司,而雇员可以拥有相关的简历

So is it better to define two additional tables (Employee, Employer) with one to one relation to User table, and have Company/CV foreign keys in there, or is it better to define User table like so 因此,最好是定义另外两个与用户表具有一对一关系的表(Employee,Employer),并在其中具有Company / CV外键,还是最好像这样定义User表

  • UserId 用户身份

    FirstName 名字

    LastName

    CompanyId - leave blank for employee (allow null) CompanyId-员工留空(允许为null)

    CVId - leave blank for employers (allow null) CVId-雇主留空(允许为空)

    RoleId 角色编号

I was thinking about adding two additional tables(employee and employer), that seems like more reasonable, but then what is the use of the Role table, and also leaving blank seems like a doable approach, just don't show the fields when adding/editing new employee/employer...But I am not sure if there is any security concerns/drawbacks when doing it like this, that is why I want to seek advice from you guys 我正在考虑添加两个其他表(员工和雇主),这似乎更合理,但是角色表的用途是什么,并且留空似乎是可行的方法,只是在添加时不显示字段/编辑新员工/雇主...但是我不确定这样做时是否有安全隐患/缺点,这就是为什么我想向你们寻求建议

Both ways are common strategies for implementing inheritance on data-model, the first one is called TPT (Table per Type), and the second one is called TPH (Table per Hierarchy). 两种方法都是在数据模型上实现继承的常见策略,第一种称为TPT(每个类型的表),第二种称为TPH(每个层次的表)。

Here's a great article that describes & compares both strategies http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx 这是一篇很棒的文章,介绍并比较了这两种策略http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx

Which strategy is the Best? 哪种策略是最好的? Trick question! 技巧问题! In isolation of your requirements there is no 'Best' strategy. 孤立您的需求时,没有“最佳”策略。 Here are some of the things you might want to consider when making your decision: 以下是您在做出决定时可能要考虑的一些事项:

  • Performance : Table Per Hierarchy is generally better performing, because no joins are necessary, everything is in one table. 性能:每个层次的表通常具有更好的性能,因为不需要任何联接,所有内容都在一个表中。 The decision becomes even more clear cut once the inheritance hierarchy gets wide or deep. 一旦继承层次变得广泛或深入,决策就变得更加清晰。
  • Flexibility : Table Per Type is often used by ISVs, as it allows customizations without modifying the 'base' table. 灵活性:ISV通常使用“每种类型的表”,因为它允许自定义而无需修改“基本”表。 ie new subtypes can be added simply by creating new tables for those sub-types. 也就是说,只需为这些子类型创建新表即可添加新的子类型。
  • Database Validation : TPH requires columns in derived types to be NULLABLE in the database, so that other derived types can be stored in the same table. 数据库验证:TPH要求派生类型中的列在数据库中为NULLABLE,以便其他派生类型可以存储在同一表中。 Because of this it is possible to create rows in the table that are not valid according to the conceptual model. 因此,可以根据概念模型在表中创建无效的行。 Ie the column is NULLABLE but the combination of a particular column being NULL and a particular discriminator or type is not valid. 也就是说,该列为NULLABLE,但是特定列为NULL和特定区分符或类型的组合无效。 This means the database is not enforcing the conceptual model for you anymore. 这意味着数据库不再为您实施概念模型。 This is fine if all access to the database is via the EF, but if anything else is used, you can end up with 'dirty' data. 如果所有对数据库的访问都是通过EF进行的,那很好,但是如果使用了其他任何方法,最终可能会得到“脏”数据。
  • Aesthetics : This one is completely subjective, but TPT feels more Object Oriented to me :) 美学:这完全是主观的,但是TPT对我来说更像是面向对象的:)
  • Storage Space : If your inheritance hierarchy has lots of types, then using TPH will result in lots of empty cells. 存储空间:如果您的继承层次结构有很多类型,那么使用TPH将导致很多空单元格。 If your database can handle 'sparse' columns well this probably isn't a real concern. 如果您的数据库可以很好地处理“稀疏”列,那么这可能不是真正的问题。

As you can see once you know what it is you are looking for it should be a pretty easy task to choose a strategy. 正如您所看到的,一旦知道要寻找的是什么,选择策略应该是一件很容易的事情。 Most of the time the recommendation is TPH because generally performance trumps these other concerns. 在大多数情况下,建议使用TPH,因为通常情况下,性能胜过其他方面的考虑。 But every situation is different, and the key is to understand exactly what you value, and then make the decision accordingly. 但是每种情况都是不同的,关键是要准确地了解您的价值,然后做出相应的决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM