【问题标题】:Extending AspNetUsers table with migration使用迁移扩展 AspNetUsers 表
【发布时间】:2016-08-17 12:51:08
【问题描述】:

我正在尝试向 Asp.NET Identity 创建的 AspNetUsers 表添加其他字段。我正在使用实体框架并遵循本指南。 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 方法。 当我尝试启动该站点时,我还看到这些列不存在,但我假设这是因为没有发生迁移。有谁知道为什么我的更改没有被检测到。

【问题讨论】:

  • 您是否更新了 AppDbContext 以反映这些变化?如果您没有更改 AppDbContext,则 up 和 down 方法将为空。

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


【解决方案1】:

你有多少个数据库?如果您使用两个数据库,请确保您指向正确的数据库。 Enable-Migrations,Enable-Migrations -ContextTypeName DatabaseService.Models.MyDbContext 正如How do I enable EF migrations for multiple contexts to separate databases? 中指出的那样

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

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


    }

【讨论】:

    【解决方案2】:

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

    Add-Migration -configuration yourapp.migrationsfolder.Configuration migrationname-Force

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2015-01-29
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2014-12-28
      • 2019-06-19
      • 2022-01-13
      相关资源
      最近更新 更多