【问题标题】:Extending the identity table with enum column使用枚举列扩展身份表
【发布时间】:2020-09-14 13:10:37
【问题描述】:

我正在使用 ASP.Net 开发一个 Web 应用程序。我正在尝试将枚举类型的新列添加到名为 AccountStatus(活动、非活动、过期和锁定)的 AspNetUsers。

我正在关注“向身份用户添加属性”的教程,所以我做了以下操作:

在 Register.cshtml.cs 文件中,我添加了以下行:

public enum AccountStatus { Active,InActive,Expired,Locked}

我创建了继承自 IdentityUsers 的“ApplicationUser”类,如下所示:

using System;
using Microsoft.AspNetCore.Identity;
namespace RegisterTest.Models
{
    public class ApplicationUser : IdentityUser
    {
        public enum AccountStatus { Active, InActive, Expired, Locked }
    }
} 

然后,我在这里添加了 DbSet:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using RegisterTest.Models;

namespace RegisterTest.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<ApplicationUser> ApplicationUser { get; set; }

    }
}

然后,我添加了迁移并更新了数据库,但实际上我得到的是一个名为“鉴别器”的新字段

谁能帮我解决这个问题?我不知道我是否遗漏了任何概念或做错了什么? 我需要将 AccountStatus 作为枚举字段添加到 Identity 用户表中。 非常感谢任何帮助。

【问题讨论】:

  • 您能否确认应用程序在您生成迁移之前已重新构建?您能否包含此更改生成的迁移?
  • 是的,它在迁移前重建。迁移刚刚添加了名为“discriminator”的新字段

标签: asp.net asp.net-mvc entity-framework asp.net-core asp.net-identity


【解决方案1】:

尝试在 DbContext 中为枚举设置转换,如下所示:

        modelBuilder
        .Entity<Wine>()
        .Property(e => e.WineVariantId)
        .HasConversion<int>();

此代码取自 https://entityframeworkcore.com/knowledge-base/50375357/how-to-create-a-table-corresponding-to-enum-in-ef-core-code-first-。看起来除了简单地在模型中定义它之外,首先将枚举设置为代码。

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多