gpt4 book ai didi

c# - 实体类型 'IdentityUserLogin' 需要定义主键

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

<分区>

我在 linux 上使用 dotnet core 1.1,当我想从我的常规 dbContext 中分离出 identityContext 时遇到问题,每当我在我的 startup.cs 中运行以下行时 --> 配置:

//... some other services
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
//running other database.migrations here + seeding data. But it is the line above that causes problems

因此这一行抛出异常:实体类型“IdentityUserLogin”需要定义主键

我根本不明白这一点,为什么我的工作是给 IdentityUserLogin 一个主键??,它是第 3 方类,我什至没有碰过它。我有以下简单设置:

namespace EsportshubApi.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

public ApplicationDbContext()
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);


}
}
}

和应用程序用户:

namespace EsportshubApi.Models.Entities
{

public class ApplicationUser : IdentityUser
{
public ApplicationUser() { }

public static ApplicationUserBuilder Builder()
{
return new ApplicationUserBuilder(new ApplicationUser());
}

public int AccountId { get; set; }
public Guid AccountGuid { get; set; }
public string Salt { get; set; }
public bool Verified { get; set; }
public string Checksum { get; set; }
public string Password { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}

}

在我的初创公司中,我通过以下方式配置身份框架:

配置服务:

services.AddEntityFrameworkSqlServer().AddMySQL().AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(config["ConnectionStrings:DefaultConnection"]));

配置:

 app.UseIdentity();

我的项目开源于:my github repo

如果有帮助。

我尝试了很多东西。两个最有前途的方法是派生在这个通用节目中使用的所有类,并显式传递它们,试图将它们的所有键更改为 int 等。这给出了与 int 完全相同的错误而不是字符串。我尝试的另一种方法是在 OnModelCreating 中执行以下操作,例如通过以下方式为 IdentityUserLogin 提供主键:

 modelBuilder.Entity<IdentityUserLogin<int>>()
.Property(login => login.UserId)
.ForMySQLHasColumnType("PK")
.UseSqlServerIdentityColumn()
.UseMySQLAutoIncrementColumn("AI");

如您所见,这是我将 UserId 作为整数时的情况,但我什至不确定 UserId 是否应该是它的主键。我可以得到其他错误而不是这个错误,它说

IdentityUserLogin is part of a hierarchy, and it has no discriminator values

但如果我有鉴别器值,它最终只会回到这个错误。我认为最奇怪的部分是我有与 UnicornStore github 示例完全相同的实现,它也使用了一些身份框架......所以我真的需要你们的帮助。可以通过下载项目重现此错误,将 default.appsettings.json 复制到 appsettings.json 中,放入有效的连接字符串,dotnet restore , 使用 dotnet run --environment Development 运行。

我什至尝试更改实现以使用 MSSQL 数据库而不是 MySQL,但这给出了完全相同的错误。

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