The entity type 'AspNetUserLogins' requires a primary key to be defined?

asp.net-core c# entity-framework entity-framework-6 identity

Question

I scaffolded my database with EF and I changed the mydbcontext to inherit from IdentityDbContext because I want everything under one dbcontext.

The problem is that when I start the application and write the credentials in the login page, I get an exception:

The entity type 'AspNetUserLogins' requires a primary key to be defined

which I didn't get before, because I am calling the base.OnModelCreating(modelBuilder); What am I doing wrong?

public partial class AdventuresContext : IdentityDbContext<ApplicationUser> 
{
    public AdventuresContext()
    {
    }

    public AdventuresContext(DbContextOptions<AdventuresContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Adventures> Adventures { get; set; }
    public virtual DbSet<AspNetRoleClaims> AspNetRoleClaims { get; set; }
    public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
    public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
    public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
    public virtual DbSet<AspNetUserRoles> AspNetUserRoles { get; set; }
    public virtual DbSet<AspNetUserTokens> AspNetUserTokens { get; set; }
    public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures");
        }
    }

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

        modelBuilder.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");

        modelBuilder.Entity<Adventures>(entity =>
        {
            entity.HasIndex(e => e.UserId);

            entity.Property(e => e.CountryCodeIso03from).HasColumnName("CountryCodeISO03From");

            entity.Property(e => e.CountryCodeIso03to).HasColumnName("CountryCodeISO03To");

            entity.Property(e => e.Name).HasMaxLength(50);

            entity.Property(e => e.ShortDescription).HasMaxLength(128);

            entity.Property(e => e.Timestamp).IsRowVersion();

            entity.Property(e => e.UserId).IsRequired();

            entity.HasOne(d => d.User)
                .WithMany(p => p.Adventures)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Adventures_Adventures");
        });
     }
 }
1
0
12/24/2019 7:42:42 AM

Popular Answer

Since you inherit from IdentityDbContext, there is no need for you to recreate AspNet* DbSet,I suggest you remove all the built-in DbSet<AspNet*> and modelBuilder.Entity<AspNet*>.

public partial class AdventuresContext : IdentityDbContext<ApplicationUser>
{
    public AdventuresContext ()
    {
    }

    public AdventuresContext (DbContextOptions<AdventuresContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Adventures> Adventures { get; set; }
    //remove below DbSet
    //public virtual DbSet<AspNetRoleClaims> AspNetRoleClaims { get; set; }
    //public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
    //public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
    //public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
    //public virtual DbSet<AspNetUserRoles> AspNetUserRoles { get; set; }
    //public virtual DbSet<AspNetUserTokens> AspNetUserTokens { get; set; }
    //public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        //...
    } 
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddRazorPages();

        var conn = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Adventures";
        services.AddDbContext<AdventuresContext>(options =>
            options.UseSqlServer(conn));
        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<AdventuresContext>();
    }
0
12/26/2019 7:49:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow