简体   繁体   English

具有我自己的枚举类型的数据库中的模型类

[英]model classes in a database with my own enum types

I have an application that I need to query lifetables (for insurance calculation). 我有一个需要查询生命表的应用程序(用于保险计算)。 I was thinking about using XML to store the data, but thought it was a little big, but maybe a little small for using a full-fledged database. 我当时在考虑使用XML来存储数据,但认为它有点大,但对于使用完整的数据库来说可能有点小。 So I chose to use SQLite. 所以我选择使用SQLite。

In my application, I have enums defining a few different things. 在我的应用程序中,我有一些枚举定义了一些不同的东西。 For example, GENDER.Male, GENDER.Female. 例如,GENDER.Male,GENDER.Female。 and JOBTYPE.BlueCollar, JOBTYPE.WhiteCollar. 和JOBTYPE.BlueCollar,JOBTYPE.WhiteCollar。 etc etc. 等等等

I have some methods that look like this: (example) 我有一些看起来像这样的方法:(示例)

FindLifeExpectancy(int age, GENDER gender);
FindDeathRate(int age, JOBTYPE jobType);

So my question is: How do you model enums in a database? 所以我的问题是:如何在数据库中建模枚举? I don't think it is best practice to use 0 or 1 in the database to store JOBTYPE because that would be meaningless to anyone looking at it. 我认为在数据库中使用0或1来存储JOBTYPE并不是最佳实践,因为这对于任何查看它的人都是没有意义的。 But if you used nvarchar, to store "BlueCollar", there would be a lot of duplicate data. 但是,如果您使用nvarchar来存储“ BlueCollar”,则会有很多重复数据。

I don't think GENDER or JOBTYPE should have an entire class, or be apart of the entity model because of the little information they provide. 我不认为GENDER或JOBTYPE应该具有整个类,也不属于实体模型,因为它们提供的信息很少。

How is this normally done? 通常如何做?

Thanks. 谢谢。

I prefer to statically map my enums in my program to a lookup table in my database. 我更喜欢将程序中的枚举静态映射到数据库中的查找表。 I rarely actually use the lookup table to do a join. 我很少实际使用查找表进行联接。 As an example I might have the following tables: 例如,我可能有以下表格:

Gender
GenderID  Name
1         Male
2         Female

Accounts
AccountID  GenderID  FirstName  LastName
1          1         Andrew     Siemer
2          2         Jessica    Siemer

And in code I would then have my enum defined with the appropriate mapping 然后在代码中,我将使用适当的映射定义枚举

public enum Gender
{
    Male = 1,
    Female = 2
}

Then I can use my enum in code and when I need to use the enum in a LINQ to SQL query I just get its physical value like this 然后,我可以在代码中使用我的枚举,并且当我需要在LINQ to SQL查询中使用该枚举时,我就可以像这样获得它的物理值

int genderValue = (int)Enum.Parse(typeof(Gender), Gender.Male));

This method may make some folks out there a bit queezy though given that you have just coupled your code to values in your database! 尽管您刚刚将代码耦合到数据库中的值,但是此方法可能会使某些人感到有些奇怪! But this method makes working with your code and the data that backs that code much easier. 但是,此方法使处理代码和支持该代码的数据更加容易。 Generally, if someone swaps out the ID of a lookup table, you are gonna be hosed in some way or another given that it is mapped across your database any how! 通常,如果有人换出了一个查询表的ID,则无论如何它都会在整个数据库中映射,将以某种方式使您烦恼! I prefer the readability and ubiquitous nature of this design though. 我更喜欢这种设计的可读性和无处不在的性质。

While it's unlikely that you will be adding a new gender, I wouldn't be so sure about the jobtype enum. 尽管您不太可能添加新的性别,但我不确定工作类型枚举。 I'd have used a separate table for both, and have foreign keys to this table every where I need to reference them. 我会为两者使用一个单独的表,并且在需要引用它们的每个位置都有该表的外键。 The schema will be extensible, the database will automatically check that only possible values are saved in the referencing tables. 模式将是可扩展的,数据库将自动检查引用表中是否仅保存了可能的值。

The SQL equivalent of 'enums' are lookup tables . 与SQL等效的“枚举”是查询表 These are tables with two (sometimes more) columns: 这些表具有两列(有时更多):

  • a code, typically short, numeric or character (ex: 'R', 'S', 'M'...) 代码,通常是短代码,数字代码或字符(例如:“ R”,“ S”,“ M” ...)
  • a text definition (ex: 'Retired', 'Student', 'Military'...) 文字定义(例如:“退休”,“学生”,“军事” ...)
  • extra columns can be used to store definitions, or alternate versions of the text for example a short abbreviation for columnar reports) 多余的列可用于存储定义,或文本的替代版本,例如列式报告的缩写)

The short code is the type of value stored in the database, avoiding the replication you mentioned. 短代码是存储在数据库中的值的类型,避免了您提到的复制。 For relatively established categories (say Male/Female), you may just use a code, without 'documenting' it in a lookup table. 对于相对确定的类别(例如,男性/女性),您可以仅使用代码,而无需在查询表中“记录”该代码。

If you have very many different codes, it may be preferable to keep their lookup in a single SQL table, rather than having a proliferation of dozen of tables. 如果您有很多不同的代码,则最好将它们的查询保存在单个SQL表中,而不要增加数十个表。 You can simply add a column that is the "category", which itself is a code, designating the nature of the group of codes defined in this category ("marital status", "employment", "education"...) 您可以简单地添加一列“类别”,其本身就是一个代码,指定此类别中定义的代码组的性质(“婚姻状况”,“就业”,“教育” ...)

The info from the lookup tables can be used to populate drop downs and such, in the UI, wherey the end-user sees the clear text but the application can use the code to query the database. 查找表中的信息可用于填充下拉列表,例如,在UI中,最终用户可以在其中看到明文,但应用程序可以使用该代码来查询数据库。 It is also used in the reverse direction, to produce the clear text for codes found in the database, for displaying results list and such. 它也可以反方向使用,以生成数据库中找到的代码的明文,以显示结果列表等。

A JOIN construct at the level of SQL is a convenient way to relate the lookup table and the main table. SQL级别的JOIN构造是将查找表和主表相关联的便捷方法。 For example: 例如:

SELECT Name, Dob, M.MaritalStatus
FROM tblCustomers C
LEFT OUTER JOIN tblMaritalLkup M ON C.MStatus = M.Code
WHERE ...

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

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