0

I want to store a set of enums in NHibernate, possibly in delimited format using | or ,. I'd like to do it flat, that is, no associated tables (which is overkill for my situation).

Now, I can use an enum and store one selection (i.e., drop down or radio button) with no problem:

public enum Test
{
    Choice1, Choice2, Choice3
}

public virtual Test? TestRadioButtons { get; set }

If the user picks Choice1, that will be saved in the database by NHibernate. But if I want the option for multiple selections, using the Test enum above, such as a check box list, I am having no luck:

public virtual List<Test> TestCheckBoxList { get; set; }

or

public virtual List<string> TestCheckBoxList { get; set; }

will throw errors (I get a "table doesn't exist" error for the former, and Generic.List vs. generic.icollection or some variation for the latter). If a user picks "Choice1" and "Choice2" from the checkbox list, only "Choice1" is saved. Again, for my situation, creating separate tables is overkill.

Is it impossible to store multiple enums as some sort of delimited list in NHibernate?

4

1 に答える 1

2

You're going to need to use a Flag enum, and store the combined value into the database as a single value. See this answer for an example and explanation.

This will make it much harder to read by humans, since most of us don't automatically translate a number into its equivalent bit sequence. However, it should make the conversion happen automatically in your code.

于 2013-03-05T00:06:25.120 に答える