代码之家  ›  专栏  ›  技术社区  ›  Jamil

C标识无法创建自定义属性

  •  0
  • Jamil  · 技术社区  · 7 年前

        public class User : IdentityUser
    {
        public string Extension { get; set; }
    }
    

    public class SecurityDbContext : IdentityDbContext<User>
    {
        private string connectionString;
        private string dbProvider;
    
        public SecurityDbContext(string connectionString, string dbProvider)
        {
            this.connectionString = connectionString;
            this.dbProvider = dbProvider;
        }
    

    在startup.cs中

    services.AddDbContext<SecurityDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("dataContext")));
    
            services.AddIdentity<User, IdentityRole>()
            .AddEntityFrameworkStores<SecurityDbContext>()
            .AddSignInManager<SignInManager<User>>()
            .AddDefaultTokenProviders();
    

    EnsureDatabasesCreated();
    

    我做错什么了?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Pablo Recalde    7 年前

    您还必须使上下文继承自 IdentityDbContext<TUser> 在哪里? TUser 您的自定义用户类型( User 在您提供的代码中)也继承自 IdentityUser ,比如:

    public class ApplicationDbContext : IdentityDbContext<User>
    {
         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
         {
         }
    }
    

    这是一个网络核心应用程序吗?如果是,请确保您也在您的 ServiceProvider 并在 ConfigureServices 方法:

     public void ConfigureServices(IServiceCollection services)
     {
         ...
         services.AddIdentity<User, IdentityRole>()
             .AddEntityFrameworkStores<ApplicationDbContext>()
             .AddSignInManager<SignInManager<User>>()
             .AddUserManager<UserManager<User>>();
         ...
     }
    

    指定自定义 用户 类和自定义 Role 如果需要的话。

        2
  •  0
  •   Jamil    7 年前

    结果我不得不将这个字段添加到我的注册请求中。