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?

rae1
  • 6,066
  • 4
  • 27
  • 48
M E Moriarty
  • 231
  • 1
  • 6
  • 15
  • 1
    Can you map the enum to separate columns instead? Unless the value truly is *opaque information* and/or *not a flag*, storing it as a single column can complicate queries later .. –  Mar 05 '13 at 00:09
  • Good point on the queries. However, I haven't the slightest idea of how to store in separate columns. Lemme do some more research. – M E Moriarty Mar 05 '13 at 00:15

1 Answers1

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.

Community
  • 1
  • 1
Bobson
  • 13,498
  • 5
  • 55
  • 80
  • But just doing `public virtual int TestCheckBoxList { get; set; }` is not enough. Would I have to create a "proxy" to do the conversion? How might I go about doing that? I am not mapping anything, but doing it through code rather. – M E Moriarty Mar 05 '13 at 00:20
  • 1
    @MEMoriarty - What do you mean by "not enough"? You'd just do `TestCheckBoxList = Test.Choice1 | Test.Choice3;` to set it. – Bobson Mar 05 '13 at 00:34
  • That's the problem I am having: I don't know how to set it. Sorry, bad grammer, it should have been: "Is doing `public virtual int TestCheckBoxList { get; set; }` enough?" to save it in DB. Could you provide an example of where I would set - I am a n00b to NH. I am getting thrown as to where i might do `TestCheckBoxList = Test.Choice1 | Test.Choice3;` – M E Moriarty Mar 05 '13 at 15:21
  • @MEMoriarty - Well, it'll depend on where the values you want to store are coming from. You say you want to store a set of enums - where are you getting that set? – Bobson Mar 05 '13 at 15:35
  • The `enum` "Test" is coming from a .cs file under a folder called "Enums". The file itself is called "TestEnums.cs" – M E Moriarty Mar 05 '13 at 15:44
  • 1
    @MEMoriarty - No, where's the *data* coming from? Are you hardcoding it? Does the user pick options from a [CheckBoxList](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx)? Do the values get computed based on other data? Basically, somewhere in your code you say "Save this to the database". That's where you want to set the value of `TestCheckBoxList`. – Bobson Mar 05 '13 at 18:54
  • Ahh, ok. Sorry for the confusion, yes, I can now figure out where to do that. For context, the user is picking from the CheckBoxList and those selected choices are what I want to store. Thanks! – M E Moriarty Mar 06 '13 at 15:09