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

时间:2015-07-08 22:39:06

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

所以我成功地获得了创建和初始化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.

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

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

3 个答案:

答案 0 :(得分:7)

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

function slider_function( $atts ) {
$atts = extract( shortcode_atts( 
                array( 
                    'tittle'=>'', 
                    'description'=>'',
                    'button'=>'',
                    'button_url'=>''
                ),$atts ) );
return '
<div class="slider">
    <h1>"' . $atts['tittle'] . '"</h1>
    <h2>"' . $atts['tittle'] . '"</h2>
    <a href="' . $atts['tittle'] . '" class="btn-modern text-center"></a>
</div>';
}

add_shortcode( 'slider','slider_function' );

这应该足够了。

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

答案 1 :(得分:0)

您可以通过添加此

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

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

Source

答案 2 :(得分:0)

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