实体类型'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin <string>'需要定义键

Ola*_* D. 21 c# asp.net-mvc asp.net-identity entity-framework-core

我有一个使用EF7的ASP.NET5 MVC应用程序.它到目前为止工作得很好,我能够在数据库中添加迁移和持久数据.现在,在将Identity添加到我的数据层项目后,我在尝试添加新迁移时遇到此错误:

实体类型'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin'需要定义一个键

我的上下文源自IdentityDbContext:

public class ASSWebApiContext : IdentityDbContext<AppUser>
Run Code Online (Sandbox Code Playgroud)

AppUser类:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这里所做的只是加载相关的新包:"Microsoft.AspNet.Identity.EntityFramework":"3.0.0-rc1-final",添加了AppUser类 - 没有别的.我有一个类似的项目使用beta-8使用完全相同的模式,它没有问题.beta-8和rc-1之间是否有任何相关变化?

谢谢!

以下是ASSWebApiContext的一部分.对于大多数具有DbSet的实体,都有一个modelBuilder.Entity.所以这个文件还有很长一段时间......

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);
                });
...
Run Code Online (Sandbox Code Playgroud)

Muh*_*hid 38

基本上,Identity表的键是在OnModelCreating方法中映射的IdentityDbContext,如果没有调用此方法,最终会得到你得到的错误.如果您从代码中派生IdentityDbContext并提供自己的定义,则不会调用此方法OnModelCreating.使用此设置,您必须显式调用using 语句的OnModelCreating方法. 这个答案还讨论了我在这里发布的选项IdentityDbContextbase.OnModelCreating