An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Scheme already exists: Identity.Application
With Entity Framework Core 2.0.0 libraries, I was able to add multiple Identity instances and create a migration.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentity<DistributorApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DistributorApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentity<SupplierApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<SupplierApplicationDbContext>()
.AddDefaultTokenProviders();
I added a library that used Entity Framework Core 2.0.1 and I upgraded all of my libraries to 2.0.1. Now, the above codes produces
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Scheme already exists: Identity.Application
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time
When I try to create a migration.
I am using the format:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
If one package references 2.0.1 and the other packages reference 2.0.0, I get the following build error:
Severity Code Description Project File Line Suppression State
Error CS1705 Assembly 'Microsoft.EntityFrameworkCore.Relational' with identity 'Microsoft.EntityFrameworkCore.Relational, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' uses 'Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.EntityFrameworkCore' with identity 'Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' SM.DAL w:\documents\visual studio 2017\Projects\SelectMarketing\SM.DAL\CSC 1 Active
If all projects reference 2.0.0, I get the following migration error:
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Scheme already exists: Identity.Application
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
At one point, I had three instances of Identity and was able to create three, separate migrations.
Apparently, when I was able to create the migrations, initially, I only had one services.AddIdentity() call. Adding three of them, even though they use different ApplicationUser classes and different ApplicationDbContext, produces the error:
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Scheme already exists: Identity.Application
Is there a way to accomplish? I have three, completely separate sets of Identity tables, in the database, that are mapped to new table names. Each set of tables is for a different MVC app, but I want to manage all the users from a single MVC app.
AddIdentityCore bypasses the default auth setup but that means more boilerplate code. Paste your startup for others to see.
@dallancarr
Currently, I have:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<DistributorApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<SupplierApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<SMContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<MigrationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentity<DistributorApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DistributorApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentity<SupplierApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<SupplierApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
x.BufferBodyLengthLimit = int.MaxValue;
});
services.AddMvc()
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
}
How do I use services.AddIdentityCore()?
Thanks.
@dallancarr I tried the following startup code and still get the error: "InvalidOperationException: Scheme already exists: Identity.Application."
IdentityBuilder builderSelect = services.AddIdentityCore<ApplicationUser>(options =>
{
// ...
});
builderSelect = new IdentityBuilder(builderSelect.UserType, typeof(IdentityRole), builderSelect.Services)
.AddDefaultTokenProviders();
builderSelect.AddSignInManager<SignInManager<ApplicationUser>>();
builderSelect.AddRoleValidator<RoleValidator<IdentityRole>>();
builderSelect.AddRoleManager<RoleManager<IdentityRole>>();
builderSelect.AddEntityFrameworkStores<ApplicationDbContext>();
IdentityBuilder builderDistributor = services.AddIdentityCore<DistributorApplicationUser>(options =>
{
// ...
});
builderDistributor = new IdentityBuilder(builderDistributor.UserType, typeof(IdentityRole), builderDistributor.Services)
.AddDefaultTokenProviders();
builderDistributor.AddSignInManager<SignInManager<DistributorApplicationUser>>();
builderDistributor.AddRoleValidator<RoleValidator<IdentityRole>>();
builderDistributor.AddRoleManager<RoleManager<IdentityRole>>();
builderDistributor.AddEntityFrameworkStores<DistributorApplicationDbContext>();
IdentityBuilder builderSupplier = services.AddIdentityCore<SupplierApplicationUser>(options =>
{
// ...
});
builderSupplier = new IdentityBuilder(builderSupplier.UserType, typeof(IdentityRole), builderSupplier.Services)
.AddDefaultTokenProviders();
builderSupplier.AddSignInManager<SignInManager<SupplierApplicationUser>>();
builderSupplier.AddRoleValidator<RoleValidator<IdentityRole>>();
builderSupplier.AddRoleManager<RoleManager<IdentityRole>>();
builderSupplier.AddEntityFrameworkStores<SupplierApplicationDbContext>();
There is no need to new up IdentityBuilder as AddIdentityCore is doing that but that is still not your issue. Looks like you are still configuring authentication somewhere.
For the sake of clarity someone has already posted a related stack question: https://stackoverflow.com/questions/47433269/multiple-identities-in-asp-net-core-2-0
I resolved this by making the following calls:
```
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
IdentityBuilder builderDistributor = services.AddIdentityCore<DistributorApplicationUser>(options =>
{
// ...
});
builderDistributor = new IdentityBuilder(builderDistributor.UserType, typeof(IdentityRole), builderDistributor.Services)
.AddDefaultTokenProviders();
builderDistributor.AddSignInManager<SignInManager<DistributorApplicationUser>>();
builderDistributor.AddRoleValidator<RoleValidator<IdentityRole>>();
builderDistributor.AddRoleManager<RoleManager<IdentityRole>>();
builderDistributor.AddEntityFrameworkStores<DistributorApplicationDbContext>();
IdentityBuilder builderSupplier = services.AddIdentityCore<SupplierApplicationUser>(options =>
{
// ...
});
builderSupplier = new IdentityBuilder(builderSupplier.UserType, typeof(IdentityRole), builderSupplier.Services)
.AddDefaultTokenProviders();
builderSupplier.AddSignInManager<SignInManager<SupplierApplicationUser>>();
builderSupplier.AddRoleValidator<RoleValidator<IdentityRole>>();
builderSupplier.AddRoleManager<RoleManager<IdentityRole>>();
builderSupplier.AddEntityFrameworkStores<SupplierApplicationDbContext>();
Glad you sorted the problem...but small refactor might be in order. 😊
services.AddIdentityCore<SupplierApplicationUser>()
.AddDefaultTokenProviders()
.AddRoles<IdentityRole>()
.AddSignInManager<SignInManager<SupplierApplicationUser>>()
.AddEntityFrameworkStores<SupplierApplicationDbContext>();