【问题标题】:Access is denied while trying to migrate EntityFramework6 mdf file - wrong path尝试迁移 EntityFramework6 mdf 文件时拒绝访问 - 路径错误
【发布时间】:2017-07-06 06:55:05
【问题描述】:

我正在尝试使用 EntityFramework6。我使用的是代码优先方法,并且在我的 projectfolder/bin/debug/SchoolContext.mdf 中创建了一个数据库。该数据库在创建时具有适当的表,并且我还能够向其中添加数据。

我在教程中达到了可以使用迁移来更新字段的一点。所以我从包管理器中启用了迁移,从表中删除了一个字段并使用Add-Migration MigrationName 生成了迁移。生成了迁移,但是当我尝试使用 Update-Database 运行它时,我得到以下信息:

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\SchoolContext.mdf'.

...

问题是它在错误的路径中寻找 mdf 文件。我试图找到如何解决这个问题,但我做不到。

正确的路径是:C:\Users\home\Documents\Visual Studio 2017\Projects\Test2\Test2\bin\Debug\SchoolContext.mdf

这是我自动生成的 App.config 文件 + 我对添加连接字符串的修改。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=bcsfdadsa56145" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|SchoolContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
 </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

我确实在配置文件中设置了DataDirectory:

internal sealed class Configuration : DbMigrationsConfiguration<Test2.Data.SchoolContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
...

【问题讨论】:

  • 您确定配置是经过验证的吗?我问它是因为内部。您是在Database.SetInitializer 上指定它吗?
  • 我在 c# 中对整个迁移和数据库的经验是有限的。我不记得在任何地方都使用过 set initializer,但如果我在配置中设置断点并运行项目,它就会停止。
  • 我认为 DataDirectory 不是正确的方法。我不知道您的要求,但您也可以创建一个指定连接的上下文或以其他几种方式。还要检查这个stackoverflow.com/questions/29440405/…
  • @bubi 你可以看这里github.com/iDaniel/WPFTest。我尝试了一些东西,所以没有很多文件。

标签: .net entity-framework database-migration


【解决方案1】:

Code First 允许您停止使用基于文件路径的连接字符串,转而使用更简单的使用数据库名称的连接字符串:

<connectionStrings> 
    <add name="SchoolContext"  
         connectionString="Server=(LocalDB)\MSSQLLocalDB;Database=School;Integrated Security=True;Connect Timeout=30" 
         providerName="System.Data.SqlClient"  /> 
</connectionStrings>

不需要配置数据库的路径,只需要数据库名即可。您可以从 Configuration 类中删除 AppDomain.CurrentDomain.SetData() 代码。

一种确保应用程序运行时以及包管理器控制台在设计时使用正确连接字符串的简单方法:DbContext 类应该有一个无参数构造函数,它使用连接字符串调用基本构造函数app.config 文件中的名称:

public MyDbContext() : base("SchoolContext") 
{ ... }

这是指定连接数据的最简单方法。如in this msdn reference page 所述,EF 6 提供了其他方法来执行此操作。

您的数据库文件将存储在哪里?这已经不重要了。 This page 解释它。但请注意,LocalDB 并非旨在用于生产系统,仅用于开发环境。

【讨论】:

  • 感谢您的回答。我正在指定文件路径,因此数据库保存在 app 文件夹中。使用您的配置,文件将保存在 C:\Users\yourusername 中,我不希望这样。
  • 我明白你的意思。然后您可以尝试使用SQLLocalDBcommand 工具将您的LocalDB 默认路径设置为与您的用户配置文件不同的位置。请注意,这也会更改每个其他项目的路径,因此最好从您的特定项目中指定一个文件夹,例如C:\Users\home\Documents\Visual Studio 2017\Projects\Databases(也许您不希望这样)。 This post 包含详细说明。
  • 要尝试的另一件事:运行Update-Database 时,在参数-ConnectionString 中提供特定的连接字符串。我不确定它是否会得到|DataDirectory| 配置,您可能必须用真实路径替换该部分。
猜你喜欢
  • 2015-03-19
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多