[英]Entity Framework code first update-database fails on CREATE DATABASE
This post has been noted 这篇文章已被注意到
On my dev machine I am trying to recreate my database using update-database from package manager console. 在我的开发机器上,我尝试使用包管理器控制台中的update-database重新创建我的数据库。 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.
物理文件名'\\ WRDatabase.mdf'可能不正确。 Diagnose and correct additional errors, and retry the operation.
诊断并更正其他错误,然后重试该操作。 CREATE DATABASE failed.
CREATE DATABASE失败。 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. 请注意,在上面的命令中,我传递了一个连接字符串名称,并且看起来正在使用连接字符串,因为.mdf文件的名称出现在错误消息中。
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). 有趣的是,我能够运行我的代码并且我的数据库已经创建,但它是用错误的名称创建的(它名为“VIN.DataModel.WRContext”,它是DbContext的完整命名空间和类名)。 My guess is that when my code runs EF cant find the connection string for creating the database.
我的猜测是,当我的代码运行时,EF无法找到用于创建数据库的连接字符串。 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 (ie I will have two connection strings for the same DbContext).
这是故意的,因为我将此上下文映射到在服务器上运行的数据库以及在本地计算机上运行的db的副本(即,我将为同一个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. 在Configuration类的构造函数中,AutomaticMigrationsEnabled设置为false。
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/ http://kazimnami.azurewebsites.net/techblog/2012/11/24/error-a-file-activation-error-occurred-create-database-failed/
Thank you kazim. 谢谢kazim。
The exception is thrown when the "|DataDirectory|" “| 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());
Actually you need to put the line given by OP's answer in the extended xxContext: dbContext
class. 实际上你需要将OP的答案给出的行放在扩展的
xxContext: dbContext
类中。
public class WRContext : DbContext
{
public WRContext()
: base("localWR")
{
AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
}
public DbSet<Emp> Emps { get; set; }
//....
}
如果以前的答案是正确的,很容易通过命令行设置它
Update-Database -AppDomainBaseDirectory C:\you-path
For me the issue was that I moved from a (localdb) instance to a solid SQL server instance. 对我来说,问题是我从一个(localdb)实例转移到一个可靠的SQL服务器实例。 In order to have update-database create the needed database i had to remove
;AttachDBFilename=|DataDirectory|\\somedatabase.mdf
为了让update-database创建我必须删除的所需数据库
;AttachDBFilename=|DataDirectory|\\somedatabase.mdf
completely then everything else worked as expected 完全然后其他一切按预期工作
If your doing integration tests then use a generic controller method to set the directory. 如果您正在进行集成测试,那么使用通用控制器方法来设置目录。 See StefanG's answer.
请参阅StefanG的回答。
public static T SetupController<T>() where T : ApiController, new()
{
//LocalDB database to the tests directory.
AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
var request = new HttpRequestMessage();
var config = WebApiConfig.SetupRegister(new HttpConfiguration());
var controller = new T()
{
Request = request,
Configuration = config
};
return controller;
}
In my case it was just that the folder that it wanted to put the database files in didn't exist. 在我的情况下,它只是想要放入数据库文件的文件夹不存在。 Apparently visual studio can't create a directory for you for some reason.
显然,visual studio无法出于某种原因为您创建目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.