阻止 EF 尝试创建初始数据库

Stop EF from trying to create initial DB

所以我成功地得到了创建和初始化身份数据库的解决方案。现在的问题是,在我处理它的过程中,我不断收到此错误

Cannot create file 'C:\Webs\SomeScheduler\SomeScheduler\App_Data\SomeSchedulerUsers.mdf' 
because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in 
the code.

Exception Details: System.Data.SqlClient.SqlException: Cannot create file 
'C:\Webs\SomeScheduler\SomeScheduler\App_Data\SomeSchedulerUsers.mdf' 
because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

我知道它已经存在了,这就是我想要的。 我如何阻止它在我每次尝试 运行 我开发此网站时尝试重新创建它?

一如既往,我们非常感谢您的帮助。

将以下行添加到您的上下文构造函数

Database.SetInitializer<YourContextClass>(null);

这应该够了。

但是如果你只想创建不存在的数据库并正确配置迁移,请看一下这个问题How to avoid recreate an existing database using automatic Code First Migration

您或许可以添加这个

<contexts> 
 <context type="Blogging.BlogContext, MyAssembly" disableDatabaseInitialization="true" /> 
</contexts>

给你的Web.config。当然更改 Blogging.BlogContext, MyAssembly 以更准确地反映您的模型。

Source

尝试响应该消息的第一件事是在 VS 中查看服务器资源管理器并右键单击数据库。如果 Detach Database 菜单项未被禁用,则单击它。当我收到这些消息时,这为我解决了问题。

我是新手,这是我第一次 post 访问此站点,但我希望我的 post 能帮助到别人。我也有同样的信息,很长一段时间我都不明白这是怎么回事。

Cannot create file '' because it already exists. Change the file path or the file name, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

我使用 MS sql 数据库和 Entity Framework(代码优先)创建了一个 asp.net mvc 项目。创建数据库后,我立即打开 MS SQL Server Management Studio 添加数据库。关闭并重新打开后 Visual Studio 我总是看到同样的错误(如果我不关闭 VS 一切正常)。原来是连接到 MS SQL Server Management Studio 引起的。关闭程序或禁用连接是不够的。 这是需要做的事情。 MS SQL SMS

脱机后,问题立即消失。我不知道它与什么有关(我的知识不够),但我希望我有所帮助。 我希望这对某人有所帮助。