.net 尝试迁移EntityFramework6 mdf文件时拒绝访问-路径错误

.net 尝试迁移EntityFramework6 mdf文件时拒绝访问-路径错误,.net,entity-framework,database-migration,.net,Entity Framework,Database Migration,我正在尝试使用EntityFramework 6。我使用代码优先的方法,在projectfolder/bin/debug/SchoolContext.mdf中创建了一个数据库。数据库在创建时有适当的表,我还能够向其中添加数据 在教程中,我达到了可以使用迁移来更新字段的程度。因此,我从package manager中启用了迁移,从表中删除了一个字段,并使用Add migration MigrationName生成了迁移。已生成迁移,但当我尝试使用更新数据库运行迁移时,会得到以下结果: CREATE

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

在教程中,我达到了可以使用迁移来更新字段的程度。因此,我从package manager中启用了迁移,从表中删除了一个字段,并使用
Add migration MigrationName
生成了迁移。已生成迁移,但当我尝试使用
更新数据库
运行迁移时,会得到以下结果:

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\visualstudio 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>

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

<connectionStrings> 
    <add name="SchoolContext"  
         connectionString="Server=(LocalDB)\MSSQLLocalDB;Database=School;Integrated Security=True;Connect Timeout=30" 
         providerName="System.Data.SqlClient"  /> 
</connectionStrings>
这是指定连接数据的最简单方法。如前所述,EF6提供了其他方法


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

您确定已指示配置吗?我问这个问题是因为内部原因。您是在
数据库.SetInitializer
中指定的吗?我在c#中使用整个迁移和数据库的经验是有限的。我不记得在任何地方使用过set初始值设定项,但如果我在配置中设置断点并运行项目,它就会停止。我认为DataDirectory不是正确的方法。我不知道您的需求,但您也可以创建一个指定连接的上下文,或者以其他几种方式。也可以查看这个@bubi,您可以在这里查看。我尝试了一些东西,所以文件不多。谢谢回答。我指定了文件路径,因此数据库保存在应用程序文件夹中。根据您的配置,文件将保存在
C:\Users\yourusername
中,我不希望这样。我明白您的意思。然后,您可以尝试使用
SQLLocalDB
命令工具将
LocalDB
默认路径设置为与用户配置文件不同的位置。请注意,这也会更改其他每个项目的路径,因此最好从特定项目中指定一个文件夹,例如
C:\Users\home\Documents\visualstudio 2017\Projects\Databases
(可能您不希望这样)。包含详细说明。另一件要尝试的事情是:运行
更新数据库时
在参数
-ConnectionString
中提供特定的连接字符串。我不确定它是否会得到
| DataDirectory |
配置,您可能必须用实际路径替换该部分。
<connectionStrings> 
    <add name="SchoolContext"  
         connectionString="Server=(LocalDB)\MSSQLLocalDB;Database=School;Integrated Security=True;Connect Timeout=30" 
         providerName="System.Data.SqlClient"  /> 
</connectionStrings>
public MyDbContext() : base("SchoolContext") 
{ ... }