简体   繁体   中英

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. When they were saved in a client record, the full string value was used, and not the ID. I have to keep this behaviour.

My question is: what is the best way to map these in my domain model using NHibernate?

I cannot change the structure of the database, and there are many list tables (I'd say about 40).

I normally use code generation to create C# Enums from the relevant database tables:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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