gpt4 book ai didi

c# - 实体类型 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin' 需要定义键

转载 作者:IT王子 更新时间:2023-10-29 04:42:24 24 4
gpt4 key购买 nike

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

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

我的上下文来自 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; }
}
}

项目.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有没有相关的变化?

谢谢!

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

最佳答案

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

关于c# - 实体类型 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>' 需要定义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34000091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com