In Identity.API, I deleted the migrations folder and then use "Add-Migration Initial" command want to generate migrations ,an error ocurred,
the error is "unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728"
i want to known how to generate this migrations files.
Hi @dupeng0224, Why did you do that? 😳
That's definitely not an app issue which is this scope of the issues in this repo, but I'll try to give you some guidance anyway.
You get that error because to generate migrations you need either:
Since ApplicationDbContext constructor has a parameter, you have to use options 2 or 3. See the details in the article: https://docs.microsoft.com/ef/core/miscellaneous/cli/dbcontext-creation
But option 2 (the one originally used) doesn't work any more, because the program startup was changed to improve logging.
And, since there's no DesignTimeFactory for ApplicationDbContext you just won't be able to create migrations at this time.
If you want to learn about this way for creating migrations you can:
Hope this helps.
Hi @mvelosop thanks for you response.
I just want to learn how to generate these migrations.
follow your instruction, i successed created ApplicationDBContext migration.
but there is another two problems,
public class PersistedGrantDbContextFactory : IDesignTimeDbContextFactory<PersistedGrantDbContext>
{
public PersistedGrantDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<PersistedGrantDbContext>();
optionsBuilder.UseSqlServer("Server=192.168.31.39;Database=Microsoft.eShopOnContainers.Services.IdentityDb;User Id=sa;Password=123456q!;");
return new PersistedGrantDbContext(optionsBuilder.Options, null);
}
}
in this factory , PersistedGrantDbContext has two params, how to define or pick the second param.
and same to PersistedGrantDbContext, the ConfigurationDbContext has the same question.
That's something internal to IdentityServer that I haven't explored!
Anyway, you should find it easily exploring by yourself the IdentityServer repos: https://github.com/IdentityServer
Hope this helps!
hello
I had same Problem
You must insert the below code in ConfigureServices Method in Startup class :
services.AddDbContext<DataContext.WebAppContext>
(options=> options.UseSqlServer(Configuration.GetConnectionString("WebAppContext")));
So You have this:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DataContext.WebAppContext>
(options=> options.UseSqlServer(Configuration.GetConnectionString("WebAppContext")));
}
Solution for someone who have multiple projects:
I had the same issue and solved it by selecting one project as startup project instead of multiple projects.
When you move your DbContext to another project you should do this:
if you use visual studio:
right click on your main project and choose "Set as startup project"
then run tools>nuget package manager>package manager console
make sure you have chosen the project which contains your DbContext (from drop-down list)
and run: Add-Migration .....
if you use dotnet-cli just type this in terminal:
dotnet ef migrations add MigrationName -s mainProject -p DbContextProject
make sure in termial you are in the directory which contains both main and DbContext project
That doesn't work, the same issue occures.

Hi, I had same issue as @Marcel-B. Fixed by providing parameterless constructor for the database context as stated in the error: MissingMethodException: No parameterless constructor defined ...
This was after migration to latest dotnet-ef 3.*. Also there is no need for DesignTimeFactory anymore.
That doesn't work, the same issue occures.
On this image we can see that your DbContext name is WebContext and CLI was able to find it, which is good. and I'm sure the problem is what I said earlier,and it seems you just didn't do it correctly.
Let me explain with more details:
imagine you have a directory named "Container"
inside it you have 2 projects:
"Main" project which must be your startup project and contains startup.cs and etc
"DataAccess" project which contains your dbContext( in your case WebContext)
you should be on Container directory in Terminal
then type this:
C:\Container> dotnet ef migrations add MyMigration -s Main -p DataAccess
just be sure you have created your DbContext in standard way. something like this:
using Microsoft.EntityFrameworkCore;
namespace Container.DataAccess.Data
{
public class WebContext : DbContext
{
public WebContext(DbContextOptions<WebContext> options)
: base(options)
{
}
}
}
Same Problem:
public void ConfigureServices(IServiceCollection services)
{
var databaseConnectionString = Configuration.GetConnectionString("AppDatabase");
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(databaseConnectionString));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>();
}
Resolve:
.AddEntityFrameworkStores<IdentityDbContext>();
change to
.AddEntityFrameworkStores<ApplicationDbContext>();
thanks for all warm-hearted friends, and this is an old problem in 2.2. i have not try it in 3.1.
You get that error because to generate migrations you need either:
- A DbContext with a default constructor (that is, a parameterless constructor)
I busted my head with million different solution, but this one worked like knife through butter.
I'am using WinForms with PostgreSQL NpqSQL provider for EF, and i thought that problem is with that specific setup.
THANK YOU!!!
Well guys, in my case I was doing dependency injection before executing the new Migration, then I commented on the startup the new injection, ran the migration and then I enabled the dependency injection.
This happened to me, but I will explain what happened.
I have a solution with several projects, one of which is webAPI and the entire ddd stack under it (application, domain, crosscutting and data)
I added the same solution to a razor page project ... and put the solution to start the 2 projects, because I wanted to see the site calling the webAPI ...
then I tried to run the migration and this problem occurred.
I had to put WebAPI, which has the project that accesses the database to be the startup and when I ran the migration it worked.
Solution for someone who have multiple projects:
I had the same issue and solved it by selecting one project as startup project instead of multiple projects.
Context: your solution is separated by projects, where the entity / database model is in theirs.
If for some reason you needed to delete the Migration folder, and when trying to generate the model again, you got this error because you excluded the Migrations folder after adding more projects that need to be started (usually along with the web api, as another site, angular, mvc, etc.).
If this is your context, a possible solution is this:
1) Indicate the web api project as the initialization;
2) In the model project, apply the add-migration commands "DesignatedName" and then the update-database command;
Searching the Web, I found this post because I had the same problem, but then I realized that I had excluded the Migrations folder after including other projects.
I followed the steps above and it worked for me.
I faced similar problem with my project. I had to do the following steps to get Add-Migration command working with EntityFrameworkCore 3.1.7
Microsoft.EntityFrameworkCore.Design package to the WebApi project Not sure why the 3rd step is required?
When i tried create first migration i have the same issue. In my case I solved it just by adding empty constructor to ApplicationDbContext.
In my case I had multiple json files configured in Program.cs as below, and I was not able to run migration commands:
````cs
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("file1.json", optional: true, reloadOnChange: true)
.AddJsonFile("file2.json", optional: true, reloadOnChange: true)
.AddJsonFile("file3.json", optional: true, reloadOnChange: true)
.Build();
CreateHostBuilder(args, configuration).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseConfiguration(configuration);
webBuilder.UseStartup
});
````
Then, with reference to this reply, I changed the implementation as below and all migration commands worked as expected:
````cs
public static void Main(string[] args) =>
CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, config) =>
{
config.SetBasePath(AppContext.BaseDirectory);
config.AddJsonFile("file1.json", optional: true, reloadOnChange: true);
config.AddJsonFile("file2.json", optional: true, reloadOnChange: true);
config.AddJsonFile("file3.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup
});
}
````
I'm using .net 5.0
I had this issue when renaming/removing the default CreateHostBuilder function in Program.cs so I switched back to default.
Most helpful comment
Hi @dupeng0224, Why did you do that? 😳
That's definitely not an app issue which is this scope of the issues in this repo, but I'll try to give you some guidance anyway.
You get that error because to generate migrations you need either:
Since ApplicationDbContext constructor has a parameter, you have to use options 2 or 3. See the details in the article: https://docs.microsoft.com/ef/core/miscellaneous/cli/dbcontext-creation
But option 2 (the one originally used) doesn't work any more, because the program startup was changed to improve logging.
And, since there's no DesignTimeFactory for ApplicationDbContext you just won't be able to create migrations at this time.
If you want to learn about this way for creating migrations you can:
Hope this helps.