gpt4 book ai didi

c# - 'MyIdentityModels.User' 违反了类型 'TUser' 的约束

转载 作者:行者123 更新时间:2023-11-30 15:19:44 26 4
gpt4 key购买 nike

我正在尝试自定义 ASP.NET Identity Core,但出现以下异常:(它似乎构建良好,但甚至在启动之前就崩溃了)

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: GenericArguments[0], 'Echap.Models.Models.Identity.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'. ---> System.TypeLoadException: GenericArguments[0], 'Echap.Models.Models.Identity.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type parameter 'TUser'.
at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.Instantiate(Type[] inst)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
--- End of inner exception stack trace ---
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType)
at Microsoft.Extensions.DependencyInjection.IdentityEntityFrameworkBuilderExtensions.AddEntityFrameworkStores[TContext,TKey](IdentityBuilder builder)
at Echap.Startup.ConfigureServices(IServiceCollection services)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection exportServices)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Echap.Program.Main(String[] args)

据我所知,我做的一切都是对的,为什么会这样?

public sealed class ApplicationDbContext : IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
try
{
Database.EnsureCreated();
}
catch (Exception e)
{
}
}

public DbSet<Category> Categories { get; set; }
public DbSet<CategoryService> CategoryServices { get; set; }
public DbSet<Coating> Coatings { get; set; }
public DbSet<Corner> Corners { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Press> Presses { get; set; }
public DbSet<PressData> PressDatas { get; set; }
public DbSet<PressService> PressServices { get; set; }
public DbSet<PrintedSide> PrintedSides { get; set; }
public DbSet<PrintSize> PrintSizes { get; set; }
public DbSet<Quantity> Quantities { get; set; }
public DbSet<Service> Services { get; set; }
public DbSet<ServiceCoating> ServiceCoatings { get; set; }
public DbSet<ServiceCorner> ServiceCorners { get; set; }
public DbSet<ServicePrintedSide> ServicePrintedSides { get; set; }
public DbSet<ServicePrintSize> ServicePrintSizes { get; set; }
public DbSet<ServiceQuantity> ServiceQuantities { get; set; }


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


modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<UserRole>().ToTable("UserClaims");
modelBuilder.Entity<UserRole>().ToTable("UserRoles");
modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
modelBuilder.Entity<RoleClaim>().ToTable("RoleClaims");
modelBuilder.Entity<UserToken>().ToTable("UserToken");
}
}

这是我的用户类:

public class User : IdentityUser<int, UserClaim, UserRole, UserLogin>
{
public User()
{
Presses = new List<Press>();
}

public string TestField { get; set; }

public virtual ICollection<Press> Presses { get; set; }
}

这是我的角色类:

public class Role : IdentityRole<int, UserRole, RoleClaim> {}

这就是我在 Startup.cs 中添加标识的方式:

services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();

最佳答案

我通过进行以下更改设法解决了这个问题:

用户.cs

public class User : IdentityUser<int>

角色.cs

public class Role : IdentityRole<int> {}

应用程序数据库上下文:

public sealed class ApplicationDbContext : IdentityDbContext<User, Role, int>

我还在 ApplicationDbContext 中添加了:

modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<IdentityUserClaim<int>>().ToTable("UserClaims");
modelBuilder.Entity<IdentityUserRole<int>>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin<int>>().ToTable("UserLogins");
modelBuilder.Entity<IdentityRoleClaim<int>>().ToTable("RoleClaims");
modelBuilder.Entity<IdentityUserToken<int>>().ToTable("UserToken");

关于c# - 'MyIdentityModels.User' 违反了类型 'TUser' 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41776804/

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