简体   繁体   English

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

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

I have a DataBase First Website in mvc. 我在mvc中有一个数据库优先网站。

My Problem is that i want to add some Points to the AspNetUser and be able to show/modify them. 我的问题是我想向AspNetUser添加一些点并能够显示/修改它们。 For example when register. 例如注册时。

So i added for example age to the AspNetUser updated the Database. 因此,我在例如AspNetUser中添加了age来更新数据库。 Then i went to the edmx DataBase and updated my Model so far so good. 然后我去了edmx数据库并到目前为止更新了我的模型。 Everything is working till this point. 一切工作到现在为止。

Now i went to the IdentityModel and added the age. 现在我去了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;
    }
}

Then i went to the RegisterViewModel 然后我去了RegisterViewModel

public class RegisterViewModel
{
.....

    public int Age { get; set; }
}

Now when i try to start the Website and to Login, as soon as i press Login i get the Folowing error. 现在,当我尝试启动网站并登录时,一旦按登录,就会出现以下错误。

The model backing the 'ApplicationDbContext' context has changed since the database was created. 自创建数据库以来,支持“ ApplicationDbContext”上下文的模型已更改。 Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 ). 考虑使用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:             {

As soon as i comment out the last two changes everything works again.I´m wondering what i`ve done wrong. 当我注释掉最后两个更改后,一切都会再次起作用。我想知道自己做错了什么。

In order for your project to work with the additional properties you need to add a "Migration" in order that Entity Framework Code First can update your database automatically- 为了使您的项目能够使用其他属性,您需要添加“迁移”,以便Entity Framework Code First可以自动更新数据库,

  1. Open the NuGet package manager console from the "Tools" menu in Visual Studio. 从Visual Studio的“工具”菜单中打开NuGet程序包管理器控制台。

  2. Type "add-migration AddedUserProperties" 键入“ add-migrationAddedUserProperties”

  3. Type "update-database" 键入“更新数据库”

See Code First Migrations on MSDN for more information. 有关更多信息,请参见MSDN上的代码优先迁移

Depending on your project configuration you may need to type "enable-migrations" which will set up code first migrations for you before this can work (if it is set up already you should have a "Migrations" directory in your project). 根据您的项目配置,您可能需要键入“ enable-migrations”,这将为您设置代码优先迁移,然后才能起作用(如果已经设置,则您的项目中应该有一个“ Migrations”目录)。

It is also possible to set your code first context to drop and recreate the database if the model has changed, or simply every time you run your code. 如果模型已更改,或者仅在每次运行代码时,也可以将代码优先上下文设置为删除并重新创建数据库。

Obviously dropping the database is going to delete any users already registered, so this is probably best done in the early prototyping stage and with "DropCreateDatabaseWhenModelChanges" initialization rather than "DropCreateDatabaseAlways", or by seeding your test users so that these are automatically recreated with the database. 显然,删除数据库将删除已注册的所有用户,因此,最好在原型设计的早期阶段,并使用“ DropCreateDatabaseWhenModelChanges”初始化而不是“ DropCreateDatabaseAlways”,或者通过播种测试用户,以便使用数据库。

To do this - 去做这个 -

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

See Understanding Database Initializers in Entity Framework Code First for more. 有关更多信息,请参见首先了解实体框架代码中的数据库初始化程序

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

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