I am trying to hook up persistence to my identity server by adding entity framework. Currently, when trying to add a migration I am receiving the error
No DbContext named 'ConfigurationDbContext' was found.
Before running the migration, I have cd
'd into the directory my .csproj file sits in and am running dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
to attempt to add the migration.
My Startup.cs
class looks as follows:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var migrationsAssembly = typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
db => db.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
db => db.MigrationsAssembly(migrationsAssembly));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseMvc();
}
}
How can I resolve this issue?
EDIT: After further investigation, when i use the --project
flag when generating the migration as follows: dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration --project BleacherBuddy.IdentityServerService
, I receive the error:
MSBUILD : error MSB1009: Project file does not exist. Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project
My current guess is that because this is a service fabric project (stateless .net core), the build process is failing here and not allowing me to generate the migration. After some research, I'm still unsure how to overcome this or if this is the actual issue. I recreated the simple identity server project as a web api (not using service fabric) and I was able to generate the classes as expected. All help is appreciated.
dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
In this command you explicitly ask dotnet ef
to use ConfigurationDbContext
class as a database context. You don't have it in your Startup.cs
, so I assume you have it elsewhere. In that case you'll need to give a fully qualified class name to the tool, including the namespace, so your command should look like this:
dotnet ef migrations add InitConfigration -c Fully.Qualified.Namespaces.ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
- replace Fully.Qualified.Namespaces.
with the actual path to your ConfigurationDbContext
class.
UPD:
Alternatively, since you actually setup your identity service with ApplicationDbContext
as EF store, you might need to use the same context in your command:
dotnet ef migrations add InitConfigration -c ApplicationDbContext -o Data/Migrations/IdentityServer/Configuration
In this case you also might need to specify fully qualified namespace before the context class name.
Here's what I did. Installed the following packages:
IdentityServer4
IdentityServer4.AspNetIdentity
IdentityServer4.EntityFramework
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
And then, instead of just building the web project with these references, I ran the project and stopped it and then tried the
dotnet ef database update --context ConfigurationDbContext
command and it worked.
I am guessing the issue could be with generating the object files or a complete NuGet package restore.
I am using Identity Server 4. After making all kind of configurations in code, I visited this headline.
The point to note is that trying from Visual Studio will not help. So, it is better to open your IdentityServer project directory in command prompt and try following commands first
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
and then try following
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb
You will get your issue resolved out.
The issue will happen in three cases:
Even you are putting your migrations in separate class library so in this case you have to implement the design time interface and follow the Microsoft instructions.
Or you are installing this Nuget package to a class library by mistake IdentityServer4.EntityFrameWork
.
You may have the docker-compose enable via visual studio which may cause the same issue as i described here: Add-Migration while using database in separate Docker container throws an error
In all cases make sure to clean then build the solution before run the command again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With