停止EF尝试创建初始DB

本文关键字:DB 创建 EF 停止 | 更新日期: 2023-09-27 18:01:37

所以我成功地得到了创建和初始化Identity数据库的解决方案。现在的问题是,当我在工作时,我一直得到这个错误

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.

我知道它已经存在了,这就是我想要的。我如何阻止它试图重新创建它,每次我试图运行我的代码,因为我开发这个网站?

一如既往,我们非常感谢任何帮助。

停止EF尝试创建初始DB

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

Database.SetInitializer<YourContextClass>(null);

这应该足够了。

但是,如果您希望仅在数据库不存在的情况下创建数据库并正确配置迁移,请查看这个问题如何使用自动代码优先迁移来避免重新创建现有数据库

你也许可以不加这个

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

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

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

我是一个新手,这是我在这个网站上的第一个帖子,但我希望我的帖子能帮助别人。我也有同样的信息,很长一段时间我都不明白发生了什么。

不能创建文件",因为它已经存在。请修改文件路径或文件名后重试。创建数据库失败。无法创建列出的一些文件名。检查相关错误

我创建了一个asp.net mvc项目与ms sql数据库和实体框架(代码第一)。创建完数据库后,我立即打开MS SQL Server Management Studio,添加了数据库。关闭和重新打开Visual Studio后,我总是看到同样的错误(如果我不关闭VS一切正常工作)。这是由于连接到MS SQL Server管理工作室造成的。仅仅关闭程序或禁用连接是不够的。这是需要做的。MS SQL SMS

离线后,问题立即消失。我不知道这与什么有关(我的知识不够),但我希望我对你有所帮助。