0

I'm using EF June CTP which has simple enum support. I have a MVC 3 view which has checkboxes that when submitted are retrieved as an array of some enum.

public enum CheckBox : byte
{
   VALUE_1 = 1,
   VALUE_2 = 2,
   ...
}

I need to take that colelction of enums ((IEnumerable)) and store that via code-first approach. Right now, that property is being ignored altogether.

I'm not sure if it would ultimately, when works, would create a separate one-to-many table or store all the values in one column but any ideas would be great. Just need to store it. Once it stores it, it would be ultimately be awesome if it didn't create a separate one-to-many table and somehow store it within the same table as some delimited string but I'm reaching here probably.

Thank you.

UPDATE 1 Tried adding [Flags] to the enum to no avail. It gets completely ignored during table generation. My Model property is:

public IEnumerable<CheckBox> Values { get; set; }
TimJohnson
  • 923
  • 1
  • 9
  • 22
  • Why don't you use flags? – SLaks Sep 27 '11 at 01:35
  • Using enum documentation from the June CTP, they say to use : byte. Are you saying simply using flags will all of a sudden make EF store the values? – TimJohnson Sep 27 '11 at 01:39
  • It will store the values in a single number, like other `[Flags]` enums. – SLaks Sep 27 '11 at 01:42
  • I got you. so do I remove the : byte type or just add the Flags onto the enum? – TimJohnson Sep 27 '11 at 01:46
  • I add the Flags to the enum. No effect. My model property of IEnumerable Values is ignored during generation. I added another property on another table to make sure tables get regenerated but this table with enum collections property gets ignored. Bummer. – TimJohnson Sep 27 '11 at 02:42
  • http://msdn.microsoft.com/en-us/library/cc138362.aspx – SLaks Sep 27 '11 at 02:46

1 Answers1

0

You should use a [Flags] enum instead of a collection:

[Flags]
public enum Checboxes {
    SomeValue = 1,
    OtherValue = 2,
    ThirdValue = 4
}

public Checkboxes Boxes { get; set; }

Boxes = Checkboxes.SomeValue | Checkboxes.OtherValue;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I should probably cap my stupid questions at 2, but in the controller I receive IEnumerable. How do I take that and convert it to Value1 | Value2 | Value3? – TimJohnson Sep 27 '11 at 03:09
  • Some helpful links: 1) http://stackoverflow.com/questions/6972937/how-do-i-convert-ienumerableenum-to-enum-in-c 2) http://stackoverflow.com/questions/4171140/iterate-over-values-in-flags-enum. Thank you for helping out – TimJohnson Sep 27 '11 at 05:40