4

初始化上的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'.

如何解決此問題?

+0

你打算在RoleClaim,UserClaim,UserLogin,UserRole,UserToken中寫入任何自定義代碼嗎?如果不是,你可以簡單地從'Context:IdentityDbContext ',User:IdentityUser '和'Role:IdentityRole ' – tmg

+0

@tmg繼承我可能需要在那些編寫一些自定義代碼...這就是爲什麼我以這種方式使用它。並且MSFT在https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs#L84中添加了這個選項我現在確定我爲什麼會遇到這個問題似乎一切都很好 –

+0

我不知道錯誤來自哪裏,爲什麼我得到的建議'考慮使用IDbContextFactory' –

回答

4

我張貼了部分答案道歉,但是這將是有用的許多...

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

你的方法Startup.ConfigureServices(...)被調用,它拋出一個異常。例外情況可能發生,因爲在運行dotnet ef時,應用程序入口點通常不是Program.Main()

嘗試

dotnet ef migrations add "FirstMigration" --verbose 

這將打印錯誤消息,並且您將能夠更好地理解這個問題。

+0

學習Program.Main()不是入口點隨着dotnet ef migrations添加「FirstMigration」 - 詳細幫助我解決了這個問題。 – ttugates