gpt4 book ai didi

entity-framework - 带有 INT id 列的 MVC 6 的 ASP.Net 身份

转载 作者:行者123 更新时间:2023-12-04 07:21:44 28 4
gpt4 key购买 nike

我将 MVC6 项目与 Asp.net Identity 一起使用,并希望将 ID 列从字符串更改为 INT。我关注了这篇文章 enter link description here

我收到一条错误消息,说可以在角色和用户的 ID 列中插入一个空值,但如果我恢复到正常状态,它就可以工作。

public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}

public DbSet<PropertyManagementCompany> PMC { get; set; }
}

最佳答案

以下是如何在 Identity、Asp NET Core 和 Entity Framework Core 上使用整数列:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

public class AppDbContext : IdentityDbContext<User, Role, int>
{
public AppDbContext(DbContextOptions options) : base(options) {}
}

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<User, Role>(options => {
// ...
}).AddEntityFrameworkStores<AppDbContext, int>(); // NOTE this line
}
}

如果出现奇怪的运行时错误:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], '...Models.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type parameter 'TUser'.

这意味着您忘记在 AddEntityFrameworkStores<AppDbContext, int>() 上添加 TKey .

关于entity-framework - 带有 INT id 列的 MVC 6 的 ASP.Net 身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422292/

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