繁体   English   中英

首先将属性添加到Asp.Net 2.0身份数据库

[英]Add Properties to Asp.Net 2.0 Identity Database first

我在mvc中有一个数据库优先网站。

我的问题是我想向AspNetUser添加一些点并能够显示/修改它们。 例如注册时。

因此,我在例如AspNetUser中添加了age来更新数据库。 然后我去了edmx数据库并到目前为止更新了我的模型。 一切工作到现在为止。

现在我去了IdentityModel并增加了年龄。

public class ApplicationUser : IdentityUser
{
    public int Age { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here            
        return userIdentity;
    }
}

然后我去了RegisterViewModel

public class RegisterViewModel
{
.....

    public int Age { get; set; }
}

现在,当我尝试启动网站并登录时,一旦按登录,就会出现以下错误。

自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改。 考虑使用Code First Migrations更新数据库( http://go.microsoft.com/fwlink/?LinkId=238269 )。

Zeile 77:             // This doesn't count login failures towards account lockout
Zeile 78:             // To enable password failures to trigger account lockout, change to shouldLockout: true
Zeile 79:             var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
Zeile 80:             switch (result)
Zeile 81:             {

当我注释掉最后两个更改后,一切都会再次起作用。我想知道自己做错了什么。

为了使您的项目能够使用其他属性,您需要添加“迁移”,以便Entity Framework Code First可以自动更新数据库,

  1. 从Visual Studio的“工具”菜单中打开NuGet程序包管理器控制台。

  2. 键入“ add-migrationAddedUserProperties”

  3. 键入“更新数据库”

有关更多信息,请参见MSDN上的代码优先迁移

根据您的项目配置,您可能需要键入“ enable-migrations”,这将为您设置代码优先迁移,然后才能起作用(如果已经设置,则您的项目中应该有一个“ Migrations”目录)。

如果模型已更改,或者仅在每次运行代码时,也可以将代码优先上下文设置为删除并重新创建数据库。

显然,删除数据库将删除已注册的所有用户,因此,最好在原型设计的早期阶段,并使用“ DropCreateDatabaseWhenModelChanges”初始化而不是“ DropCreateDatabaseAlways”,或者通过播种测试用户,以便使用数据库。

去做这个 -

Database.SetInitializer(new DropCreateDatabaseWhenModelChanges<ApplicationDbContext>());

有关更多信息,请参见首先了解实体框架代码中的数据库初始化程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM