在EF core 使用枚举类型

博客介绍了在EntityFramework Core中使用枚举的方法。因EF Core实体不直接支持枚举操作,以MsgInfo实体为例,原本字段SendState被生成int类型,不能修改为枚举。可新增枚举字段SendStateEnum,添加相关标记,操作时用其代替SendState,还提到旧方法已过时,可参考新文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在EntityFramework Core中的实体是不直接支持枚举类型的操作,这让我们在开发过程中带来不少的麻烦,下面总结一下在ef core中使用枚举的方法.

例如下面的MsgInfo实体,对应着数据库表MsgInfo,其中字段SendState发送状态在业务逻辑上有发送成功发送失败两种枚举状态。但ef把它生成了int类型,而不是枚举,当然也不能修改成枚举,这样会导致ef写入和读取数据异常。

原来的实体

    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] 标记

需添加Nuget包Newtonsoft.Json

修改完的实体代码如下

修改实体

    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 
    }

添加了SendStateEnum字段后,以后使用ef core操作或者读取SendStateEnum 代替了 SendState 字段

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

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

修改后的最终实体代码如下

    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 
    }

以上方式已过时,新版本可以直接使用 https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值