简体   繁体   English

为什么 EF Core 会抛出“数据库已存在”?

[英]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);我在 .Net Core 上有一个使用 EF Core(均为 2.2)的应用程序;
  2. I have an existing database on local SQLServer;我在本地 SQLServer 上有一个现有数据库;
  3. I use built-in DI to supply my DbContext: services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString)) ;我使用内置 DI 来提供我的 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;我使用 Fluent API 来配置我的上下文(以空方法开始)和DbContextOptions<MyDbContext> options构造函数;
  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.使用所有提到的我在一个空模型上执行( dotenet ef migrations add Initial --project ../MyDALProject )以获得一个空的初始迁移。 That works fine.这很好用。 Then I run dotenet ef database update --project ../MyDALProject to apply this Empty Migration to the database.然后我运行dotenet ef database update --project ../MyDALProject将此空迁移应用到数据库。 Nevertheless, I get "Database 'TargetDatabase' already exists. Choose a different database name" .不过,我得到"Database 'TargetDatabase' already exists. Choose a different database name"

BTW.: other projects work with the same Db just as fine.顺便说一句。:其他项目也可以使用相同的 Db。

What can be the problem?可能是什么问题? How to make it works?如何使它起作用?

PS: other projects' contextes, I have mentioned, are been scaffolded and mapped to some existing tables. PS:其他项目的上下文,我已经提到,被脚手架并映射到一些现有的表。 After scaffold, migrations and updates work as expected.在脚手架之后,迁移和更新按预期工作。 The problem relys only to this project, which will bring new tables to an existing db.问题仅取决于这个项目,它将把新表带到现有的数据库中。

毕竟,我决定手动将脚本应用到 db,然后发现,db 无法访问……最后,我恢复了它,一切运行良好。

For me, I renamed the database directory which contains the mdf and logs directory.对我来说,我重命名了包含 mdf 和日志目录的数据库目录。 Had to give it back its name to get the migration working.必须将其名称归还才能使迁移正常工作。

Still inaccessibility issues.仍然无法访问的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core:更新数据库错误:关系“所有者”已经存在 - EF Core: update database error: relation "Owner" already exists EF Core Postgres Update-Database 正在尝试创建已经存在的数据库 - EF Core Postgres Update-Database is trying to Create Database which already exists EF-Core:表“名称”已存在 - 尝试更新数据库时 - EF-Core: Table "name" already exists - when trying to update database EF Core 数据库中已经有一个名为“AspNetRoles”的对象 - EF Core There is already an object named 'AspNetRoles' in the database EF为什么在“分离”时说具有相同键的对象已经存在? - Why does EF say an object with the same key already exists when it's “Detached”? 带有Sqlite内存数据库的EF Core用于测试会引发“集合为只读”错误-为什么? - EF Core with Sqlite Memory Database for testing throws “Collection is read-only” error - why? EF Core Add-Migration 错误属性已存在 - EF Core Add-Migration error property already exists Ef core code first Error: Key ("Id")=(33) already exists - Ef core code first Error : Key ("Id")=(33) already exists 如何使用已存在于 EF 核心中的子实体保存实体? - How to save an entity with a child entity which already exists in EF core? EF-Core 尝试插入已存在的关系数据 - EF-Core trying to insert relational data that already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM