迁移到 ASP.Net Core Identity 2.1 后无法添加迁移

Can not add migration after migrating to ASP.Net Core Identity 2.1

我将我的应用程序从 .Net Core 1.1、EF Core 1.1、ASP.Net Identity 1.1 迁移到所有版本 2.1。我还将自己的用户(目前称为 Diver)与 IdentityUser 分开,因此我不需要向 ASP.Net Idenentity-EF 提供自定义用户-class。

当我现在尝试添加初始迁移时:

dotnet ef migrations add initial -s . -p ../Tauchbolde.Common/

我收到错误:

The entity type 'IdentityUserLogin<int>' requires a primary key to be defined.

我的上下文是这样的:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    { }

    public DbSet<Event> Events { get; set; }
    public DbSet<Participant> Participants { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Diver> Diver { get; set; }
    public DbSet<Notification> Notifications { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<PostImage> PostImages { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // ApplicationUser
        builder.Entity<Diver>()
            .HasMany(e => e.Notificationses)
            .WithOne(e => e.Recipient)
            .OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Diver>()
            .HasMany(e => e.Comments)
            .WithOne(e => e.Author)
            .OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Diver>()
            .HasMany(e => e.Events)
            .WithOne(e => e.Organisator)
            .OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Diver>()
            .HasMany(e => e.Posts)
            .WithOne(e => e.Author)
            .OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Diver>()
            .HasMany(e => e.Claims)
            .WithOne()
            .HasForeignKey(e => e.UserId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);
        builder.Entity<Diver>()
            .HasMany(e => e.Logins)
            .WithOne()
            .HasForeignKey(e => e.UserId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);
        builder.Entity<Diver>()
            .HasMany(e => e.Roles)
            .WithOne()
            .HasForeignKey(e => e.UserId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade);

        // Comment
        builder.Entity<Comment>().HasOne(e => e.Event).WithMany(e => e.Comments).OnDelete(DeleteBehavior.Restrict);

        // Event
        builder.Entity<Event>().HasIndex(p => new { p.StartTime, p.Deleted });
        builder.Entity<Event>().Property(e => e.Deleted).HasDefaultValue(false);
        builder.Entity<Event>().Property(e => e.Canceled).HasDefaultValue(false);

        // Notification
        builder.Entity<Notification>().Property(e => e.AlreadySent).HasDefaultValue(false);
        builder.Entity<Notification>().Property(e => e.CountOfTries).HasDefaultValue(0);

        // Participants
        builder.Entity<Participant>().HasOne(e => e.Event).WithMany(e => e.Participants).OnDelete(DeleteBehavior.Restrict);
        builder.Entity<Participant>().Property(e => e.CountPeople).HasDefaultValue(1);

        // Post
        builder.Entity<Post>().HasIndex(p => new { p.Category, p.PublishDate });

        // PostImage

        // UserInfo
        builder.Entity<Diver>().Property(e => e.NotificationIntervalInHours).HasDefaultValue(1);
    }

完整的源代码可以在 GitHub 上找到。

我已经尝试了很多事情,比如为身份实体添加忽略语句。没有任何效果,我被卡住了。

有什么可能出错的想法吗?

我不确定,但尝试用这个替换你的第一行:

public class ApplicationDbContext : IdentityDbContext<IdentityUser>

看起来我的实体与 ASP.Net 身份实体之间存在一些关系。从我继承的身份用户实体切换时,我似乎没有清理它们。我还向我的 DBContext 添加了两个构造函数,它们不使用通用参数而是使用默认参数。这已经修复了它,我现在可以使用我的问题中显示的推荐来添加迁移。