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

问题描述

我有一个使用EF7的ASP.NET5 MVC应用程序.到目前为止,一切正常,我能够添加迁移并将数据持久存储在数据库中.现在,在将身份添加到我的数据层项目中之后,尝试添加新迁移时出现此错误:

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:

我的上下文是从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&lt; string&gt;"需要定义一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:53