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

asp.net-core asp.net-identity-3 c# entity-framework-core

Question

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?

1
4
9/19/2016 3:36:56 PM

Popular Answer

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.

5
9/15/2017 6:24:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow