Consider using IDbContextFactory to override DbContext initialization at design time

In 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 objects:

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

At 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 the "ConfigureServices" method when starting the "WebProject.Startup" class. Consider using IDbContextFactory to override DbContext initialization at design time. Error: GenericArguments [0], 'WebProject.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4 [TUser, TRole, TContext, TKey] violates the restriction of type "TUser".

?

+4
1

, ...

"ConfigureServices"

Startup.ConfigureServices(...) . , , , dotnet ef Program.Main(), .

    dotnet ef migrations add "FirstMigration" --verbose 

, .

+4

Source: https://habr.com/ru/post/1655070/


All Articles