Asp.net核心列表< enum>在模型中 [英] Asp.net Core List<enum> in model

查看:55
本文介绍了Asp.net核心列表< enum>在模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的模型中包含一个枚举列表,但是我遇到了一些问题.在我的第一种方法中,我尝试了以下方法:

I'm trying to include a list of enums in my model, however I'm encountering some issues. In my first approach I 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 { get { return Lat.ToString(); } }

    public string GeoLong { get { return Lon.ToString(); } }

    public List<Difficulty> Difficulty { get; set; }
}

public enum Difficulty { F, PD, AD, D, TD, ED };

但是,当我尝试使用ef执行任何操作时,以这种方式使用enum会导致异常:

However using enum in such way results with an exception when I try to perform any operation with ef:

System.InvalidOperationException:属性'Ferrata.Difficulty'无法映射,因为它的类型为列表",即不是受支持的原始类型或有效的实体类型.任何一个显式映射此属性,或忽略它.

System.InvalidOperationException: The property 'Ferrata.Difficulty' could not be mapped, because it is of type 'List' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it.

根据互联网上的一些建议,我创建了一个独立的类来保存我的枚举值,如下所示:

Following some advice on the internet, I created a standalone class for holding my enum values like this:

public class FerrataDifficulty
{
    public int ID { get; set; }
    public Difficulty Difficulty { get; set; }

    public FerrataDifficulty(Difficulty difficulty)
    {
        Difficulty = difficulty;
    }
}

将我原来的Ferrata类更改为FerrataDifficulty列表后,程序将进行编译,但是存在两个问题:*即使在数据库初始化程序中,我在调试代码时也会初始化困难,它们似乎为空*当我尝试通过应用程序删除数据库条目时,出现以下错误:

After changing my original Ferrata class to take list of FerrataDifficulty the program compiles, however there are two problems: * Even though in my database initializer I initialize the difficulties when I debug the code they seem to be null * When I try to delete the database entries through the application I get the following error:

SqlException:DELETE语句与REFERENCE约束"FK_FerrataDifficulty_Ferrata_FerrataID"发生冲突.在数据库"ViaFerrata1"的表"dbo.FerrataDifficulty"的列"FerrataID"中发生了冲突.

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_FerrataDifficulty_Ferrata_FerrataID". The conflict occurred in database "ViaFerrata1", table "dbo.FerrataDifficulty", column 'FerrataID'.

如果有人能指出我做错了什么,以及在asp.net核心模型中包含枚举列表的最佳实践是什么,我将不胜感激.

I would appreciate if anyone could point out what I'm doing wrong and what's the best practice on including a list of enums in the model in asp.net core.

推荐答案

我设法通过使用枚举标志解决了这个问题:

I managed to solve the problem by 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 to be the simplest way to achieve exactly what I needed.

这篇关于Asp.net核心列表&lt; enum&gt;在模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆