我有一个使用ASP.NET5的EF7 MVC应用程序。到目前为止,它运行得很好,我能够在数据库中添加迁移和持久化数据。现在,在向数据层项目添加标识后,在尝试添加新迁移时会出现以下错误:
实体类型'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin‘需要定义一个键。
我的上下文来自于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
{
"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使用完全相同的模式,它的工作没有问题。β-8和rc-1之间是否有任何相关的变化?
谢谢!
下面是ASSWebApiContext的一部分。对于大多数拥有modelBuilder.Entity的实体,有一个DbSet。所以文件持续了很长一段时间..。
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);
});
...发布于 2015-12-01 05:43:15
基本上,标识表的键是在OnModelCreating的IdentityDbContext方法中映射的,如果没有调用该方法,您将得到所得到的错误。如果从IdentityDbContext派生并像在代码中那样提供自己的OnModelCreating定义,则不会调用此方法。使用此设置,您必须使用OnModelCreating语句显式调用IdentityDbContext的base.OnModelCreating方法。This answer还讨论了我在这里发布的选项
发布于 2021-10-06 02:45:59
我最近研究了.NET 5,并遇到了同样的问题。在我的例子中,我为ApplicationUser、ApplicationRole和ApplicationUserRole派生了来自IdentityUser、IdentityRole和IdentityUserRole的类。
ApplicationUserRole有一个复杂的键,它是UserId和RoleId的组合,并在OnModelCreating方法中定义了相同的键。
在播种数据时,我指的是IdentityUserRole而不是ApplicationUserRole,同时向用户添加了一个角色。也造成了同样的问题。在经历了一些麻烦之后,我意识到出了什么问题。我刚把它改成了ApplicationUserRole。
发布于 2021-12-22 03:37:20
以防万一,这对我有这个问题的人有帮助,而且我疯了,因为我已经在重写OnModelCreating了。
结果是,当重写IIdentityUser时,我使用了intellisense建议的错误名称空间。
我用的是:
Microsoft.AspNet.Identity.EntityFramework
而且应该用
Microsoft.AspNetCore.Identity
https://stackoverflow.com/questions/34000091
复制相似问题