Correct posture? Is this how enumeration types are used in EF Core?

Correct posture? Is this how enumeration types are used in EF Core?

Entities in Entity Framework Core do not directly support enumeration type operations, which caused us a lot of trouble during the development process

最后更新 11/9/2021 10:06 AM
waitaction
预计阅读 3 分钟
分类
EF Core
标签
.NET C# EF Core ORM .NET Core

Entities in Entity Framework Core do not directly support enumeration type operations, which caused us a lot of trouble during the development process. Here is a summary of the methods of using enumeration in ef core.

For example, the following MsgInfo entity corresponds to the database table MsgInfo, in which the sending status of the field SendState has two enumerated states in business logic: ** Send Success ** and ** Send Failure **. But ef generated it as an int type instead of an enumeration, and certainly cannot be modified to an enumeration, which will cause ef-writing and reading data exceptions.

Original entity

public partial class MsgInfo
{
    public string Id { get; set; }
    public string UserAddress { get; set; }
    public string Content { get; set; }
    public int SendState { get; set; }
}

这里新增一个字段SendStateEnum设置为枚举类型,并使用 [NotMapped] 为不映射到数据库,为了防止输出 HTTP 时被序列化,也可以添加 [Newtonsoft.Json.JsonIgnore] 标记

Need to add NuGet package Newtonsoft.Json

The modified entity code is as follows

modified entity

public partial class MsgInfo
{
    public string Id { get; set; }
    public string UserAddress { get; set; }
    public string Content { get; set; }
    public int SendState { get; set; }

    [NotMapped]
    [Newtonsoft.Json.JsonIgnore]
    public SendStateEnum SendStateEnum
    {
        get
        {
            switch (SendState)
            {
                case (int)SendStateEnum.Fail:
                    return SendStateEnum.Fail;
                case (int)SendStateEnum.Success:
                    return SendStateEnum.Success;
                default:
                    return SendStateEnum.UnKnow;
            }
        }
        set
        {
            SendState = (int)value;
        }

    }
}
public enum SendStateEnum
{
    Success = 1,
    Fail = 2,
    UnKnow =3
}

After adding the SendStateEnum field, we will use ef core to operate or read SendStateEnum instead of the SendState field (** Note: The webmaster's actual measurement is inconsistent with the original text, such as the following code (original code) seems to be using SendState to operate EF Core. If you have any different opinions, please leave a message and correct it. **)

using (var context = new FrameworkDbContext())
{
    var result = context.MsgInfo.Where(m => m.SendStateEnum == SendStateEnum.Success);
}

当然,为了防止原来的 SendState 字段被使用,可以添加标记 [Obsolete] 提醒用户该字段 SendState 已过时。

The revised final entity code is as follows

public partial class MsgInfo
{
    public string Id { get; set; }
    public string UserAddress { get; set; }
    public string Content { get; set; }

    [Obsolete]
    public int SendState { get; set; }

    [NotMapped]
    [Newtonsoft.Json.JsonIgnore]
    public SendStateEnum SendStateEnum
    {
        get
        {
            switch (SendState)
            {
                case (int)SendStateEnum.Fail:
                    return SendStateEnum.Fail;
                case (int)SendStateEnum.Success:
                    return SendStateEnum.Success;
                default:
                    return SendStateEnum.UnKnow;
            }
        }
        set
        {
            SendState = (int)value;
        }
    }
}
public enum SendStateEnum
{
    Success = 1,
    Fail = 2,
    UnKnow =3
}
Keep Exploring

延伸阅读

更多文章