gpt4 book ai didi

c# - 考虑在设计时使用 IDbContextFactory 覆盖 DbContext 的初始化

转载 作者:太空狗 更新时间:2023-10-30 01:01:23 32 4
gpt4 key购买 nike

在 ASP.NET Core 1.0.1 项目中,使用 Entity Framework Core 和 ASP.NET Identity,我有以下上下文:

public class Context : IdentityDbContext<User, Role, Int32, UserClaim, UserRole, UserLogin, RoleClaim, UserToken> { 
public Context(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder) {
base.OnModelCreating(builder);
}
}

以及以下实体:

public class User : IdentityUser<Int32, UserClaim, UserRole, UserLogin> { }
public class Role : IdentityRole<Int32, UserRole, RoleClaim> { }
public class RoleClaim : IdentityRoleClaim<Int32> { }
public class UserClaim : IdentityUserClaim<Int32> { }
public class UserLogin : IdentityUserLogin<Int32> { }
public class UserRole : IdentityUserRole<Int32> { }
public class UserToken : IdentityUserToken<Int32> { }

在启动时我有以下内容:

services.AddDbContext<Context>(x => x.UseSqlServer(connectionString, y => y.MigrationsHistoryTable("__Migrations")));

services
.AddIdentity<User, Role>()
.AddEntityFrameworkStores<Context, Int32>()
.AddDefaultTokenProviders();

当我运行 dotnet ef migrations add "FirstMigration" 时,出现以下错误:

An error occurred while calling method 'ConfigureServices' on startup class 'WebProject.Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: GenericArguments[0], 'WebProject.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.

如何解决这个问题?

最佳答案

对于发布部分答案,我深表歉意,但这对许多人来说都是有用的......

An error occurred while calling method 'ConfigureServices' on startup class

正在调用您的方法 Startup.ConfigureServices(...) 并抛出异常。发生异常的原因可能是在运行 dotnet ef 时,应用程序入口点不像往常一样是 Program.Main()

尝试

    dotnet ef migrations add "FirstMigration" --verbose 

这将打印错误消息,您将能够更好地理解问题。

关于c# - 考虑在设计时使用 IDbContextFactory 覆盖 DbContext 的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574124/

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