c# - Asp.net Core List<enum> in model -
i'm trying include list of enums in model, i'm encountering issues. in first approach tried this:
public class ferrata { [jsonproperty(propertyname = "id")] public int id { get; set; } [jsonproperty(propertyname = "placename")] public string name { get; set; } [jsonignore] public double lat { get; set; } [jsonignore] public double lon { get; set; } public string geolat { { return lat.tostring(); } } public string geolong { { return lon.tostring(); } } public list<difficulty> difficulty { get; set; } } public enum difficulty { f, pd, ad, d, td, ed };
however using enum in such way results exception when try perform operation ef:
system.invalidoperationexception: property 'ferrata.difficulty' not mapped, because of type 'list' not supported primitive type or valid entity type. either explicitly map property, or ignore it.
following advice on internet, created standalone class holding enum values this:
public class ferratadifficulty { public int id { get; set; } public difficulty difficulty { get; set; } public ferratadifficulty(difficulty difficulty) { difficulty = difficulty; } }
after changing original ferrata class take list of ferratadifficulty program compiles, there 2 problems: * though in database initializer initialize difficulties when debug code seem null * when try delete database entries through application following error:
sqlexception: delete statement conflicted reference constraint "fk_ferratadifficulty_ferrata_ferrataid". conflict occurred in database "viaferrata1", table "dbo.ferratadifficulty", column 'ferrataid'.
i appreciate if point out i'm doing wrong , what's best practice on including list of enums in model in asp.net core.
i managed solve problem using enum flags:
[flags] [jsonconverter(typeof(newtonsoft.json.converters.stringenumconverter))]
public enum difficulty { f = 1, pd = 2, ad = 4, d = 8, td = 16, ed = 32 };
this seems simplest way achieve needed.
Comments
Post a Comment