Asp.net核心列表< enum>在模型中

编程入门 行业动态 更新时间:2024-10-23 23:21:33
本文介绍了Asp核心列表< 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核心模型中包含枚举列表的最佳实践是什么,我将不胜感激.

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 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;在模型中

本文发布于:2023-11-11 06:58:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   核心   列表   net   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!