Search code examples
c#asp.net-identity-2asp.net-core-2.1ef-core-2.1asp.net-core-mvc-2.1

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


I migrated my app from .Net Core 1.1, EF Core 1.1, ASP.Net Identity 1.1 to all version 2.1. I also separated my own user (currently called Diver) from the IdentityUser so I don't need to provide a custom user-class to ASP.Net Idenentity-EF.

When I now try to add an initial migration with:

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

I get the error:

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

My context looks like this:

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);
    }

The entire source-code can be found here on GitHub.

I tried already a lot of things like adding ignore statements for the Identity entities. Nothing did work and I am stuck.

Any ideas what can go wrong?


Solution

  • It looked like I had some relations left from my entities into the ASP.Net Identity entities. Seems I did not clean them up when switching away from my inherited Identity user entity. I also added two more constructors to my DBContext which don't use generic parameters but the default one. This has fixed it the way that I am now able to add migrations with the commend showed in my question.