在身份框架中添加新的 属性
Adding a new property in Identity Framework
首先,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 中添加了 Email 到 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: Invalid column name 'Email'.
Why am I getting this error? What am I missing? Thanks.
打开 nuget 控制台并输入:
update-database -force
在此之前,请确保您已打开自动迁移。
你可以通过
打开它
enable-migrations
首先,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 中添加了 Email 到 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: Invalid column name 'Email'. Why am I getting this error? What am I missing? Thanks.
打开 nuget 控制台并输入:
update-database -force
在此之前,请确保您已打开自动迁移。 你可以通过
打开它enable-migrations