【问题标题】:ASP.NET add-migration generates empty migration on IdentityModels.cs changesASP.NET add-migration 对 IdentityModels.cs 更改生成空迁移
【发布时间】:2017-01-05 15:40:04
【问题描述】:

我一直在对 IdentityModels.cs 进行一些更改。通常只是忽略几个字段并将表从“AspNetUsers”重命名为“Users”。但是在我保存这些更改、构建项目并生成迁移之后,它只是空的..

我对 IdentityModels.cs 所做的更改如下:

public class ApplicationUser : IdentityUser
{
    public virtual List<Bestelling> Bestellingen { 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;
    }
}

我这里基本上加了1行,public virtual List。

然后我还补充说:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().Ignore(c => c.EmailConfirmed)
                                           .Ignore(c => c.PhoneNumber)
                                           .Ignore(c => c.PhoneNumberConfirmed)
                                           .Ignore(c => c.TwoFactorEnabled)
                                           .Ignore(c => c.LockoutEnabled)
                                           .Ignore(c => c.LockoutEndDateUtc);

        modelBuilder.Entity<IdentityUser>().ToTable("Users");
    }

正如我所说,只是忽略一些字段并将表重命名为用户。 之后我只是保存了所有内容,构建了项目并输入了

add-migration database-v2

然后

update-database

但是什么也没发生,因为迁移看起来像这样:

namespace Eindopdracht.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class databasev2 : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}
}

现在我在其他模型中添加了这个位:

public virtual DbSet<Klant> Klants { get; set; }

但我不知道要在 IdentityModels.cs 中添加什么以使它们融合在一起。

我在 Stack Overflow 上查看了其他问题,但我真的找不到解决方案。

编辑

我的 ApplicationDbContext() 的内容:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("name=DatabankEntities", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().Ignore(c => c.EmailConfirmed)
                                           .Ignore(c => c.PhoneNumber)
                                           .Ignore(c => c.PhoneNumberConfirmed)
                                           .Ignore(c => c.TwoFactorEnabled)
                                           .Ignore(c => c.LockoutEnabled)
                                           .Ignore(c => c.LockoutEndDateUtc);

        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
    }
}

编辑二

这是我的 Configuration.cs:

namespace Eindopdracht.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.DatabankBestellijn>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(Eindopdracht.Models.DatabankBestellijn context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}
}

我改了

internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.DatabankBestellijn>

位到:

internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.ApplicationDbContext>

而且它有效!但是我现在不能更新数据库,因为它说这些表已经存在,有没有办法强制它?

update-database -Force

似乎不是要走的路。

最终编辑

更改 configuration.cs 文件并在 IdentityModels.cs 中进行细微更改是解决方案。感谢您的帮助

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    问题出在您的OnModelCreating 方法中

    modelBuilder.Entity<IdentityUser>().
    

    您在应该使用 ApplicationUser

    的地方引用 IdentityUser

    所以你的OnModelCreating 变成了

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().Ignore(c => c.EmailConfirmed)
                                           .Ignore(c => c.PhoneNumber)
                                           .Ignore(c => c.PhoneNumberConfirmed)
                                           .Ignore(c => c.TwoFactorEnabled)
                                           .Ignore(c => c.LockoutEnabled)
                                           .Ignore(c => c.LockoutEndDateUtc);
    
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
    }
    

    【讨论】:

    • 我已经按照你说的做了更改,但是迁移仍然是空的。我只需要在更改后构建然后添加迁移对吗?
    • 是的,没错。如果自上次生成迁移后数据库没有更改,则不会生成迁移。
    • 嗯,迁移是在 Migrations 目录中生成的,但它仍然像之前一样是空的..
    • 请问您可以编辑您的问题并显示您的 DbContext 类吗?
    • 我想知道您的 _MigrationHistory 表是否一团糟 - 尝试给迁移一个不同的名称 - 例如 add-migration database-v3
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2014-12-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2019-11-17
    相关资源
    最近更新 更多