通过迁移扩展AspNetUsers表

时间:2016-04-24 01:45:35

标签: c# asp.net asp.net-mvc entity-framework

我正在尝试将其他字段添加到由Asp.NET Identity创建的AspNetUsers表中。我正在使用Entity Framework并遵循本指南。 http://aspmvp.com/archives/37

到目前为止,我已经在IdentityModels.cs中的ApplicationUser类中添加了字段。

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int TimePlayed { get; set; }
    public float RevenueGenerated { get; set; }
    public string FavoriteCharity { get; set; }
    public string FavoriteGame { get; set; }
    public string ImageLocation { 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;
    }
}

我还更改了在AccountController.cs中注册所需的字段,但我认为这不会影响其工作原理。

现在我正在尝试将更改迁移到我的数据库,但由于某种原因,Add-Migration创建的迁移有一个空的Up和空的Down。我使用的命令是:

Enable-Migrations -EnableAutomaticMigrations
Update-Database

然后我试了

Add-Migration IdentityModels
Update-Database

两者都在迁移中创建了空的Up和Down方法。 当我尝试启动网站时,我也看到列不存在,但我认为这是因为迁移没有发生。有谁知道为什么我的变化没有被发现。

2 个答案:

答案 0 :(得分:0)

你有怎样的manay数据库?如果您使用两个数据库,请确保指向正确的数据库。 How do I enable EF migrations for multiple contexts to separate databases?

中指出的Enable-Migrations,Enable-Migrations -ContextTypeName DatabaseService.Models.MyDbContext

此外,如果您有两个数据库,我建议将它们合并为一个,

public class MySecondDBContext : IdentityDbContext<ApplicationUser>
{
    public SocialContext()
        : base("DefaultConnection")
    {


    }

答案 1 :(得分:0)

尝试在添加迁移时添加-Force。

添加 - 迁移 - 配置yourapp.migrationsfolder.Configuration migrationname-Force

相关问题