在CREATE DATABASE实体框架代码第一次更新数据库失败 [英] Entity Framework code first update-database fails on CREATE DATABASE

查看:754
本文介绍了在CREATE DATABASE实体框架代码第一次更新数据库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个帖子有有人指出



所以有这个



在我的dev的机器,我尝试使用更新的数据库从包管理器控制台重新创建我的数据库。我相信我已按照上面提到的职位说明



我收到此错误信息:




出现文件激活错误。物理文件名
'\WRDatabase.mdf'可能不正确。诊断和纠正额外的
错误,重试操作。 CREATE DATABASE失败。列出的某些文件
的名字不能被创建。检查相关的错误




我运行的命令是:



 更新数据库-ConfigurationTypeName WRConfiguration -ConnectionStringName localWR -Verbose 

请注意在上面的命令我传递一个连接字符串名称,它似乎正在被使用的连接字符串,因为.mdf文件的名称出现在错误消息。



有趣的是,我能够运行我的代码和我的数据库中创建,但它与错误的名称(它被命名为VIN.DataModel.WRContext,这是的DbContext的完整的命名空间和类名)创建。我的猜测是,我的代码运行时EF不能找到创建数据库连接字符串。这是故意的,因为我将映射这种背景下,一个服务器上运行,并到本地机器上运行的数据库副本的数据库(即我将拥有相同的DbContext两个连接字符串)。



下面是应用程序的配置:

 <&的EntityFramework GT; 
< defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework>
<&参数GT;
<参数值=11.0/>
< /参数>
< / defaultConnectionFactory>
< /&的EntityFramework GT;
<&是connectionStrings GT;
<添加名称=localWR的providerName =System.Data.SqlClient的的connectionString =数据源=(的LocalDB)\v11.0;初始目录= WRDatabase; AttachDBFilename = | DataDirectory目录| \WRDatabase。中纤板;集成安全性= TRUE; MultipleActiveResultSets = TRUE/>
< /&是connectionStrings GT;



AutomaticMigrationsEnabled设置为false在配置类的构造函数。



下面是上下文类:

 公共类WRContext:的DbContext 
{
上配置静态WRContext()
{
Database.SetInitializer(新MigrateDatabaseToLatestVersion< WRContext,VIN.DataModel.WRMigrations.WRConfiguration>());
}

公共WRContext()
{

}

公共WRContext(字符串的connectionString)
:底座(的connectionString)
{

}


公共DbSet<&的Emp GT; EMPS {搞定;组; }
公共DbSet<&INV GT; INVS {搞定;组; }
公共DbSet< WRConfig> WRConfigs {搞定;组; }
}


解决方案

我找到了答案在这里



http://kazimnami.azurewebsites.net/techblog/2012/11/24/error-a-file-activation-error-occurred-create-database-failed/



感谢您卡齐姆



的异常被抛出时,| DataDirectory目录|在连接字符串中财产不进行评估。为了解决这个问题,添加下面一行到你的代码:

  AppDomain.CurrentDomain.SetData(DataDirectory目录,System.IO。 Directory.GetCurrentDirectory()); 


This post has been noted

So has this one

On my dev machine I am trying to recreate my database using update-database from package manager console. I believe I have followed the instructions in the posts cited above.

I get this error message:

A file activation error occurred. The physical file name '\WRDatabase.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

The command I am running is:

update-database -ConfigurationTypeName WRConfiguration  -ConnectionStringName localWR -Verbose

Note in the command above I am passing a connection string name and it appears the connection string is being used because the name of the .mdf file appears in the error message.

Interestingly, I am able to run my code and my database is created but it is created with the wrong name (it is named "VIN.DataModel.WRContext" which is the full namespace and class name of the DbContext). My guess is that when my code runs EF cant find the connection string for creating the database. This is intentional as I will map this context to a database that runs on a server and also to a copy of the db that runs on the local machine (i.e. I will have two connection strings for the same DbContext).

Here is app config:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
     <add name="localWR" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog =WRDatabase;AttachDBFilename=|DataDirectory|\WRDatabase.mdf; Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>

AutomaticMigrationsEnabled is set to false in the constructor of the Configuration class.

Here is the context class:

public class WRContext : DbContext
{
    static WRContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<WRContext, VIN.DataModel.WRMigrations.WRConfiguration>());
    }

    public WRContext()
    {

    }

    public WRContext(string connectionString)
        : base(connectionString)
    { 

    }


    public DbSet<Emp> Emps { get; set; }
    public DbSet<Inv> Invs { get; set; }
    public DbSet<WRConfig> WRConfigs { get; set; }
}  

解决方案

I found the answer here:

http://kazimnami.azurewebsites.net/techblog/2012/11/24/error-a-file-activation-error-occurred-create-database-failed/

Thank you kazim.

The exception is thrown when the "|DataDirectory|" property in the connection string is not evaluated. To solve the issue, add following line to your code:

AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());

这篇关于在CREATE DATABASE实体框架代码第一次更新数据库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆