entity framework - Using enum as FK on EF 6 -


i'd use enum foreign key in code-first app. since enums stored int, thought use attribute [foreignkey] on enum property, throws exception:

the types of properties in dependent role of referential constraint  must same corresponding property types in principal role 

here example of trying do:

public enum usertype {     administrator = 1,     member = 2 }  public class user {     public int userid { get; set; }     public string login { get; set; }      [foreignkey("typedetails")]     public usertype type { get; set;}      public virtual mastertype typedetails { get; set; } }  public class mastertype {     public int mastertypeid { get; set; }     public string description { get; set; }     ... } 

is possible or similar through fluent api or migrations?

thanks

here's 1 made earlier: https://www.nuget.org/packages/ef-enum-to-lookup

it's nuget package provides method can call in seed (initializer and/or migrations) automatically build lookup tables , add fks enum used. usage info.

enjoy :-) , let me know if works (or else matter!)


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -