本文介绍了实体类型 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>'需要定义一个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 EF7 的 ASP.NET5 MVC 应用程序.到目前为止一切正常,我可以在数据库中添加迁移和持久化数据.现在,将 Identity 添加到我的数据层项目后,我在尝试添加新迁移时收到此错误:

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:

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

我的上下文来自 IdentityDbContext:

My context is derived from IdentityDbContext:

public class ASSWebApiContext : IdentityDbContext<AppUser>

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

project.json

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"
  }
}

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

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?

谢谢!

以下是 ASSWebApiContext 的一部分.大多数具有 DbSet 的实体都有一个 modelBuilder.Entity.所以文件持续了很长一段时间......

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

推荐答案

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

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

这篇关于实体类型 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>'需要定义一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:53