2

I'm using code first and migration to update my db. I have a lookup table and a correspond enum. my table: States

Id | State
---|-------
   |

My enum:

public enum States
    {
        good = 1,
        bad = 2
    }

I want to fill State table with the values of the enum, so if I change the enum values - the table will be changed in accordance. I googled a lot but couldn't find anything clear about it.

Any basic example will be appreciated.

ParPar
  • 7,355
  • 7
  • 43
  • 56

1 Answers1

2

A Google turns up the EF-Enum-To-Lookup project, which looks like it will do what you want.

Creates lookup tables and foreign key constraints based on the enums used in your model.

Install it from nuget...

Install-Package ef-enum-to-lookup

You can run this with...

var enumToLookup = new EnumToLookup();
enumToLookup.Apply(context);

You could run this from your migrations Seed method, but you'll need to be aware than adding an enum member does not change your model, so if you're using explicit migrations the Seed method won't be run.

Richard
  • 29,854
  • 11
  • 77
  • 120
  • Thanks, but I forgot to mention that I don't want to use ef-enum-to-lookup package because this package is running on ef6 + and I want it to work on ef5 as well – ParPar May 14 '15 at 13:01
  • It's open source - I suggest you either try recompiling it with EF5, or make use of the techniques used within this package to accomplish your goal. – Richard May 14 '15 at 13:35
  • 1
    I'd be interested to know if it can be made to work with ef5, and I'd be open to pull requests. – Tim Abell Jun 01 '16 at 14:56