ddeamaral
ddeamaral

Reputation: 1443

Error on running migration in MVC Core application

I'm trying to run the following command to create a new migration for a context that I created myself. I run this command.

dotnet ef migrations add MusicContextCreation --context MusicContext

Here is my music context class. This is what I'm trying to add a migration for.

using Microsoft.EntityFrameworkCore;

namespace MusicianProject.Models.Contexts
{
    public class MusicContext : DbContext
    {
        public MusicContext(DbContextOptions<MusicContext> options) : base(options)
        {

        }

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

            //builder.Entity<Band>().HasMany<BandMember>();
            //builder.Entity<Band>().HasMany<Album>(band => band.Albums);
            builder.Entity<Band>().ToTable("Band");
            //builder.Entity<BandMember>().HasOne<User>(bandmember => bandmember.User);
            builder.Entity<BandMember>().ToTable("BandMember");
            builder.Entity<Album>().ToTable("Album");
            builder.Entity<Genre>().ToTable("Genre");
        }

        public DbSet<Band> Bands { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<BandMember> BandMembers { get; set; }
        public DbSet<Album> Albums { get; set; }

    }
}

Here is the error message I'm getting in the command prompt when I run that command.

The entity type Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin requires a primary key to be defined.

What I don't understand is I have called the base classes OnModelCreating which I've seen as the answer in other similar questions. What I don't understand is that another context I have for Identity users, does not give me this issue, and it actually sets up the identity tables.

I can add the classes that are being used in this context if need be, I did not add them yet as I didn't want to clutter. Let me know in the comments if it is needed to see the classes being defined in dbset collections.

Upvotes: 0

Views: 262

Answers (1)

Dmitry
Dmitry

Reputation: 16795

You should inherit from IdentityDbContext (instead of DbContext), which has proper OnModelCreating content with fluent configuration of Identity classes.

If you want to have two different DbContexts, one for users and other for other classes - you should not have any references between classes in different contexts, read my answer here

Upvotes: 1

Related Questions