初始化上的ASP.NET核心1.0.1項目,使用實體框架的核心和ASP.NET的身份,我有以下方面:考慮使用IDbContextFactory覆蓋的DbContext的在設計時
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'.
如何解決此問題?
你打算在RoleClaim,UserClaim,UserLogin,UserRole,UserToken中寫入任何自定義代碼嗎?如果不是,你可以簡單地從'Context:IdentityDbContext',User:IdentityUser '和'Role:IdentityRole ' –
tmg
@tmg繼承我可能需要在那些編寫一些自定義代碼...這就是爲什麼我以這種方式使用它。並且MSFT在https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs#L84中添加了這個選項我現在確定我爲什麼會遇到這個問題似乎一切都很好 –
我不知道錯誤來自哪裏,爲什麼我得到的建議'考慮使用IDbContextFactory' –