Miguel Moura
Miguel Moura

Reputation: 39394

Consider using IDbContextFactory to override the initialization of the DbContext at design-time

On an ASP.NET Core 1.0.1 project, using Entity Framework Core and ASP.NET Identity, I have the following context:

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);
  }
}

And the following entities:

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> { }

On Startup I have the following:

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

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

When I run dotnet ef migrations add "FirstMigration" I get the following error:

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'.

How to solve this problem?

Upvotes: 4

Views: 4273

Answers (1)

Gerardo Grignoli
Gerardo Grignoli

Reputation: 15177

I apologise for posting a partial answer but it will be usefull for many...

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

Your method Startup.ConfigureServices(...) is being called and it is throwing an exception. The exception probably happens because when running dotnet ef the application entry point is not Program.Main() as usual.

Try

    dotnet ef migrations add "FirstMigration" --verbose 

That will print the error message and you will be able to better understand the issue.

Upvotes: 5

Related Questions