Search code examples
c#entity-frameworkef-core-2.2

Why does EF Core throws "Database already exists"?


My start-up conditions:

  1. I have an application on .Net Core using EF Core (both 2.2);
  2. I have an existing database on local SQLServer;
  3. I use built-in DI to supply my DbContext: services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));
  4. I use Fluent API to configure my context (empty method to start with) and DbContextOptions<MyDbContext> options constructor;
  5. I have several migrations from other projects existing in Database and no existing in my current project.

Using all of mentioned I perform (dotenet ef migrations add Initial --project ../MyDALProject) on an empty model to get an empty Initial Migration. That works fine. Then I run dotenet ef database update --project ../MyDALProject to apply this Empty Migration to the database. Nevertheless, I get "Database 'TargetDatabase' already exists. Choose a different database name".

BTW.: other projects work with the same Db just as fine.

What can be the problem? How to make it works?

P.S.: other projects' contextes, I have mentioned, are been scaffolded and mapped to some existing tables. After scaffold, migrations and updates work as expected. The problem relys only to this project, which will bring new tables to an existing db.


Solution

  • After all, I decided to apply scripts to db manually and then found out, that db was inaccessible... Finally, I restored it and everything ran perfectly.