简体   繁体   English

NHibernate中的映射表枚举

[英]Mapping Table Enums in NHibernate

I'm currently building a new app based on a legacy database. 我目前正在基于遗留数据库构建一个新应用。 In the app, there are many "list" tables that have the following format: 在应用程序中,有许多具有以下格式的“列表”表:

table listLanguages
{
    PrimaryId bigint;
    Item varchar(30);
}

...with some sample entries like this: ...带有以下示例条目:

PrimaryId Item
1         English
2         French
3         Spanish
...

They were used to populate dropdown lists in web forms that did things like edit client details. 它们用于填充Web表单中的下拉列表,这些列表执行诸如编辑客户端详细信息之类的操作。 When they were saved in a client record, the full string value was used, and not the ID. 将它们保存在客户记录中时,将使用完整的字符串值,而不是ID。 I have to keep this behaviour. 我必须保持这种行为。

My question is: what is the best way to map these in my domain model using NHibernate? 我的问题是:使用NHibernate在我的域模型中映射这些内容的最佳方法是什么?

I cannot change the structure of the database, and there are many list tables (I'd say about 40). 我无法更改数据库的结构,并且有很多列表表(大约40个)。

I normally use code generation to create C# Enums from the relevant database tables: 我通常使用代码生成从相关的数据库表中创建C#枚举:

public enum Language
{
    NoneSpecified = 0,
    English       = 1,
    French        = 2,
    Spanish       = 3,
    ... 
}

Have a look at T4 template code generation, built into Visual Studio 2005 onwards. 看一下Visual Studio 2005以后内置的T4模板代码生成。

Here's what i did in a project: I created a base class for these tables and created a child class for each of tables inherited from this base class then I had separate mapping files for each of tables. 这是我在项目中所做的事情:我为这些表创建了一个基类,并为从该基类继承的每个表创建了一个子类,然后为每个表创建了单独的映射文件。

Although I think there can be some more complicated solutions like using an IInterceptor to intercept load behavior or adding some mapping files in run time using Configuration class can be some options. 尽管我认为可以有一些更复杂的解决方案,例如使用IInterceptor拦截负载行为或使用Configuration类在运行时添加一些映射文件,这是一些选择。

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

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