【问题标题】:Stop EF from trying to create initial DB阻止 EF 尝试创建初始数据库
【发布时间】:2015-09-27 02:18:05
【问题描述】:

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

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.

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

与往常一样,非常感谢任何和所有帮助。

【问题讨论】:

    标签: c# asp.net entity-framework asp.net-identity


    【解决方案1】:

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

    Database.SetInitializer<YourContextClass>(null);
    

    这应该足够了。

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

    【讨论】:

      【解决方案2】:

      你也许可以不用添加这个

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

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

      Source

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-01
          • 2020-02-09
          • 2016-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-03
          • 1970-01-01
          相关资源
          最近更新 更多