I am trying to update my app from netcore3.1 to net 6.0 but on initial migration I have following error
Code
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see .....I haven't change startup.cs and program.cs, see below
C#
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
host.Run();
}
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
}and part of startup containing ApplicationDbContext
C#
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));It was working well on netcore3.1 but now I can't initialize first migration


Tuhin PaulRank #7 · 71.9K reputationPosted Feb 21, 2023, 2:50 PM EST
The error message you're encountering suggests that there may be an issue with the way the ApplicationDbContext is being created or configured during the migration process. One possible cause of this error could be a mismatch between the version of Entity Framework Core that you're using and the version of the .NET runtime that your application is targeting.
To resolve this issue, you may need to update your application's dependencies to match the new version of the .NET runtime that you're using. This can typically be done by updating the "Microsoft.EntityFrameworkCore" and "Microsoft.EntityFrameworkCore.Tools" packages in your project's NuGet package manager to their corresponding versions for .NET 6.0.
You may also need to ensure that your DbContext and any associated models or data structures are compatible with the new version of Entity Framework Core. This can sometimes require modifications to the way your database is configured or the way data is stored.
If you're still experiencing issues after updating your dependencies and ensuring compatibility with Entity Framework Core, you may want to review the specific error message and any relevant documentation to see if there are any additional steps or configurations that need to be made.
Marius VasileRank #785 · 2.9K reputationPosted Feb 21, 2023, 2:56 PM EST
Thank you for your answer, my project file is
net6.0 9.0 false all runtime; build; native; contentfiles; analyzers; buildtransitive all runtime; build; native; contentfiles; analyzers; buildtransitiveAll packages updated to latest version for net core 6.0
Jignesh KumarRank #91 · 22.9K reputationPosted Feb 21, 2023, 10:58 PM EST
Hello Marius Vasile,
Please check this one,
https://snede.net/you-dont-need-a-idesigntimedbcontextfactory/
Marius VasileRank #785 · 2.9K reputationPosted Feb 21, 2023, 11:02 PM EST
Thank ou Jignesh, my DbContext already follow
public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { Database.EnsureCreated(); }Marius VasileRank #785 · 2.9K reputationPosted Feb 22, 2023, 6:42 AM EST
My app is a razor page asp.net core. used to be net core 3.1 and I try to update to net 6.0. Below line might have a negative effect on running initial migration?
Marius VasileRank #785 · 2.9K reputationPosted Feb 22, 2023, 11:02 AM EST
It was
changed to
and now is working fine