在Identity Framework中添加新属性

本文关键字:属性 新属性 添加 Identity Framework | 更新日期: 2023-09-27 18:25:01

首先,IdentityModels.cs看起来是这样的:使用Microsoft.AspNet.Identity.EntityFramework;

namespace WebApplication2.Models
{
    public class ApplicationUser : IdentityUser
    {
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }
}

我把这个文件改成这个:

namespace WebApplication2.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string Email { get; set; }
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
            Database.SetInitializer<ApplicationDbContext>(new DropCreateDatabaseAlways<ApplicationDbContext>());
        }
    }
}

我还在AccountViewModel.cs.中向RegisterViewModel添加了电子邮件

我还将AccountController更改为:

 var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email};
                var result = await UserManager.CreateAsync(user, model.Password);

当我点击注册时,它在以下行失败:

 var result = await UserManager.CreateAsync(user, model.Password);

这样说:

System.Data.SqlClient.SqlException:列名"电子邮件"无效。为什么我会出现此错误?我错过了什么?谢谢

在Identity Framework中添加新属性

打开nuget控制台并键入:

update-database -force

在此之前,请确保已打开自动迁移。你可以通过打开它

enable-migrations