Maria Perez
Maria Perez

Reputation: 173

EF Core Migration error: "Unable to create an object of type 'ApplicationContext'"

I'm trying to do the migration with EF Core but I get an error - how can I fix this error?

PM> add-migration ini

Unable to create an object of type 'ApplicationContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

Upvotes: 14

Views: 28699

Answers (10)

Canada Wan
Canada Wan

Reputation: 415

I got this error because I have two separated projects: Data Models and ApplicationContext are defined in MyProject.Data while Program.cs is in MyProject.UI. So I have to specify them as follows:

dotnet ef migrations add v1.0 --project MyProject.Data --startup-project MyProject.UI

Upvotes: 2

Palindromer
Palindromer

Reputation: 994

It's unobvious, but this error message also appears if there are any exceptions in a DbContext constructor or in OnConfiguring method.

For example, Database.EnsureCreated() can fail because of incorrect connection string or disabled database.

Tip: to find out the problem root, just wrap your code in try catch and display your exception using Console.WriteLine();

Upvotes: 1

REDBLIX
REDBLIX

Reputation: 1

I solved this problem by adding a constructor to ApplicationContext

Upvotes: 0

I got this error when the Primary Key for the table was not set manually (with attributes or with FluentAPI) and was not determined automatically (not "Id" kayword and not "TableName" + "Id")

Stack: ASP.NET Core EF + Postgres (Npgsql)

Upvotes: 0

Jess Rod
Jess Rod

Reputation: 280

In my case, this error existed because I was applying migrations at runtime. After removing Database.Migrate(); in my code, I was able to add the new migration without errors.

By the way, I used EF Core .NET command-line interface (CLI) tools, not the Package Manager Console (I run into problems I couldn't solve when I was using the Package Manager Console, so I went for the CLI alternative, and it was worth the change).

Also, make sure you use the appropriate options to select your startup project and your target project.

Upvotes: 0

Yury Yatskov
Yury Yatskov

Reputation: 195

I got this error when creating a migration. In my code in the ApplicationContext constructor, I used the Database.EnsureCreated() method. Typically this method is used for small applications where you need to check for the existence of a database and create it once. When creating an application with a database that will change with new versions and using migrations, you should not use this method.

Upvotes: 1

Mohammad Niazmand
Mohammad Niazmand

Reputation: 1547

It is highly probably because of your default startup project:

Open Package manager console and write this command:

 PM> Add-Migration -StartupProject[YourProjectPath(just press a tab all the.csproj paths will come up, and then choose your DataBaseLayer project to execute a migration for)] migrationName

In this way you don't need to again go to solution and set startup project with your webProject whenever you finished adding a new migration.

Upvotes: 3

SamTh3D3v
SamTh3D3v

Reputation: 9944

I had the same issue with asp.net core 3.1. For me, it was pretty straight forward, adding an implementation of IDesignTimeDbContextFactory to the main web project fixed the issue.

A basic implementation looks something like this:

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<YourDbContext>
{
    public YourDbContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var builder = new DbContextOptionsBuilder<YourDbContext >();
        var connectionString = configuration.GetConnectionString("DefaultConnection");
        builder.UseSqlServer(connectionString);
        return new YourDbContext(builder.Options);
    }
}

Please refer to this blog post on the subject.

Upvotes: 9

Zeeshan Adil
Zeeshan Adil

Reputation: 2135

1.Modify your code in ConfigureService with :

options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),

        x => x.MigrationsAssembly("WebApplication")));
  1. In command line which includes WebApplication.csproj:

dotnet ef migrations add Init

Upvotes: 0

john
john

Reputation: 337

This will also happen if you have multiple start-up projects - when migrating, just select one (in VS right click Solution and Set StartUp Projects...)

Upvotes: 11

Related Questions