简体   繁体   中英

The entity type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>' requires a key to be defined

I have a ASP.NET5 MVC application using EF7. It works all fine so far and i'm able to add migrations and persist data in the database. Now after adding Identity to my data layer project I get this error when trying to add a new migration:

The entity type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin' requires a key to be defined

My context is derived from IdentityDbContext:

public class ASSWebApiContext : IdentityDbContext<AppUser>

the AppUser class:

using Microsoft.AspNet.Identity.EntityFramework;
using System;

namespace ASS.DomainDataModel.Models
{
    public class AppUser : IdentityUser
    {
        public string AppUserId { get; set; }
        public DateTime FirstFlight { get; set; }
    }
}

project.json

{
  "version": "1.0.0-*",
  "description": "ASS.DomainDataModel Class Library",
  "authors": [ "xxxx" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "frameworks": {
    "dnx451": {
      "dependencies": {
      }
    },
    "dnxcore50": {
      "dependencies": {
      }
    }
  },

  "dependencies": {
    "ASS.DomainClasses": "1.0.0-*",
    "Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Relational": "7.0.0-rc1-final",
    "System.Linq.Expressions": "4.0.11-beta-23516",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final"
  },

    "commands": {
    "ef": "EntityFramework.Commands"
  }
}

All I have done here is to load the relevant new package: "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final", added the AppUser class - nothing else. I had a similar project using beta-8 using the exact same pattern where it worked without problems. Are there any relevant changes between beta-8 and rc-1?

thanks!

Below is part of the ASSWebApiContext. There's a modelBuilder.Entity for most of the entities that have a DbSet. So the file goes on for quite a while...

using Microsoft.Data.Entity;
using ASS.DomainClasses.Entities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.PlatformAbstractions;
using System.Linq;
using ASS.DomainClasses.Interfaces;
using System;
using Microsoft.AspNet.Identity.EntityFramework;

namespace ASS.DomainDataModel.Models
{
    public class ASSWebApiContext : IdentityDbContext<AppUser>
    {
        public IConfigurationBuilder Config { get; set; }
        public IConfigurationRoot _Configuration { get; private set; }

        public ASSWebApiContext(IApplicationEnvironment appEnv)
        {
            Database.EnsureCreated();

            Config = new ConfigurationBuilder()
                .SetBasePath(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json");

            _Configuration = Config.Build();

        }

        public DbSet<Address> Addresses { get; set; }
        public DbSet<AddressType> AddressTypes { get; set; }
        public DbSet<Aircraft> Aircrafts { get; set; }
        public DbSet<AircraftModel> AircraftModels { get; set; }
        public DbSet<AircraftOwner> AircraftOwners { get; set; }
        public DbSet<AircraftOwnerType> AircraftOwnerTypes { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<GPEncodingType> GPEncodingTypes { get; set; }
        public DbSet<LocationPoint> LocationPoints { get; set; }
        public DbSet<Manufacturer> Manufacturer { get; set; }
        public DbSet<Pilot> Pilots { get; set; }
        public DbSet<ServiceProvider> ServiceProviders { get; set; }
        public DbSet<State> States { get; set; }
        public DbSet<Trip> Trips { get; set; }
        public DbSet<Stop> Stops { get; set; }
        public DbSet<Track> Tracks { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {


            modelBuilder.Entity<AddressType>(
                e =>
                {
                    e.Property(n => n.AddressTypeId).IsRequired().UseSqlServerIdentityColumn();
                    e.Property(n => n.Name).IsRequired().HasMaxLength(15);
                    e.Ignore(n => n.IsDirty);
                });

            modelBuilder.Entity<Address>(
                e =>
                {
                    e.Property(n => n.AddressId).IsRequired().UseSqlServerIdentityColumn();
                    e.Property(n => n.AddressTypeId).IsRequired();
                    e.Property(i => i.CountryId).HasMaxLength(2);
                    e.Property(i => i.AddrLine1).HasMaxLength(256);
                    e.Property(i => i.AddrLine2).HasMaxLength(256);
                    e.Property(i => i.AddrLine3).HasMaxLength(256);
                    e.Property(i => i.Postcode).HasMaxLength(50);
                    e.Ignore(n => n.IsDirty);
                });
...

Basically the keys of Identity tables are mapped in OnModelCreating method of IdentityDbContext and if this method is not called, you will end up getting the error that you got. This method is not called if you derive from IdentityDbContext and provide your own definition of OnModelCreating as you did in your code. With this setup you have to explicitly call the OnModelCreating method of IdentityDbContext using base.OnModelCreating statement. This answer also discusses the option I posted here

I recently worked on .NET 5, and I came across the same issue. In my case, I have derived classes for ApplicationUser , ApplicationRole , and ApplicationUserRole that derive from IdentityUser , IdentityRole and IdentityUserRole .

ApplicationUserRole has a complex key that is a combination of UserId, and RoleId, and defined the same in the OnModelCreating method.

Down the line while seeding the data, I was referring to IdentityUserRole instead of ApplicationUserRole while adding a role to a user. That caused the same problem. After some trouble, I had realized what was going wrong. I just changed it to ApplicationUserRole .

Just in case this helps anyone I had this issue and was going crazy as I was already overriding OnModelCreating.

It turns out that when overriding IIdentityUser I was using the wrong namespace that intellisense suggested.

I was using:

Microsoft.AspNet.Identity.EntityFramework

And should have been using

Microsoft.AspNetCore.Identity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM