Wrong path while trying to migrate EntityFramework6 mdf file RRS feed

  • Question

  • I'm trying to play around with EntityFramework6. I'm using a code first approach, and I've created a database in my projectfolder/bin/debug/SchoolContext.mdf. The database has the proper tables when created and I'm also capable of adding data to it. 

    I reached a point in the tutorial where I can use migration to update the field. So I enabled the migrations from package manager, removed a field from a table and generated the migration by using `Add-Migration MigrationName`. The migration is generated, but when I try to run it by using `Update-Database`, I get the following:

        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'.

    The problem is that it is looking for the mdf file in the wrong path. I've tried to find how to solve this, but I couldn't. 

    The right path would be: `C:\Users\home\Documents\Visual Studio 2017\Projects\Test2\Test2\bin\Debug\SchoolContext.mdf`

    Here is my auto generated App.config file + my modifications of adding a connection string. 

        <?xml version="1.0" encoding="utf-8"?>
            <!-- 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=, Culture=neutral, PublicKeyToken=bcsfdadsa56145" requirePermission="false" />
            <add name="SchoolContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|SchoolContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
                <parameter value="v11.0" />
              <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

    I do set the `DataDirectory` in the configuration file:

        internal sealed class Configuration : DbMigrationsConfiguration<Test2.Data.SchoolContext>
            public Configuration()
                AutomaticMigrationsEnabled = false;
                AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
    Thursday, July 6, 2017 8:01 AM

All replies

  • Hi Bogdan2k17,

    Based on your description and related code, I create a simple console App and reproduce your issue on my side, it seems that DataDirectory does not support and file in bin/debug folder.

    as a workaround, please use full path in your app. config file, like this:

       <add name="SchoolContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\home\Documents\Visual Studio 2017\Projects\Test2\Test2\bin\Debug\SchoolContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />

    Best regards,

    Cole Wu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 7, 2017 9:24 AM