title: How to define DataDirectory for ConnectionString in console application to work with EntityFramework Code First Migrations
body: <p>I try to set location <code>MyProject\App_Data\Cos.mdf</code> for the database in <code>App.config</code>:</p>
  
  <pre><code> &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Cos.mdf;Initial Catalog=Cos;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>In <code>Program.cs</code> I wrote:</p>
  
  <pre><code> static void Main(string[] args) {
  
          string relative = @"..\..\App_Data\Cos.mdf";
          string absolute = Path.GetFullPath(relative);
  
          AppDomain.CurrentDomain.SetData("DataDirectory", absolute); 
          Console.WriteLine(absolute);
          Console.ReadKey();
   }
  </code></pre>
  
  <p>The displayed path is(I paste it to show that I didn't make a mistake):</p>
  
  <p><img src="https://i.stack.imgur.com/nPYf0.png" alt="enter image description here"></p>
  
  <p>but then when I type in Package Manager Console <code>enable-migrations</code> change <code>AutomaticMigrations</code> to true, then type <code>update-database</code> I get error:</p>
  
  <p><code>Cannot attach the file 'C:\Users\s8359_000\Documents\Visual Studio 2013\Projects\Projekt5 — kopia\Projekt5\bin\Debug\Cos.mdf' as database 'Cos'.</code></p>
  
  <p>Why does .NET tries to create my database in <code>Debug</code> directory?! I went through 15 subjects on StackOverflow on this topic and it looks like everybody just duplicates the answers which don't work.</p>
  
  <p><strong>EDIT AFTER ANSWER OF SRUTZKY</strong>
  Yes you are right there is error. I tried few more combination after your answer, unfortunately none worked.</p>
  
  <pre><code>  &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\baza.mdf;Initial Catalog=baza;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>and the Main</p>
  
  <pre><code>  static void Main(string[] args) {
  
          Console.WriteLine("BEFORE:" + AppDomain.CurrentDomain.GetData("DataDirectory"));
          string relative = @"..\..\App_Data\Cos.mdf";
          string absolute = Path.GetFullPath(relative);
          absolute = Path.GetDirectoryName(@absolute);
          AppDomain.CurrentDomain.SetData("DataDirectory", @absolute);
          Console.WriteLine(@absolute);
          Console.WriteLine(AppDomain.CurrentDomain.GetData("DataDirectory"));
          Console.ReadKey();
  }
  </code></pre>
  
  <p>then I get in console:</p>
  
  <p><img src="https://i.stack.imgur.com/fSSLg.png" alt="enter image description here"></p>
  
  <p>and after deleting <code>Migrations</code> directory and <code>enable-migrations</code>, automatic migrations to true, <code>update-database</code> I get:</p>
  
  <blockquote>
    <p>PM> update-database Specify the '-Verbose' flag to view the SQL
    statements being applied to the target database.
    System.Data.SqlClient.SqlException (0x80131904): A file activation
    error occurred. The physical file name '\baza.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.    at
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
    Boolean breakConnection, Action<code>1 wrapCloseInAction)    at
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
    exception, Boolean breakConnection, Action</code>1 wrapCloseInAction)    at
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
    stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)    at
    System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,
    SqlCommand cmdHandler, SqlDataReader dataStream,
    BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
    stateObj, Boolean&amp; dataReady)    at
    System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
    methodName, Boolean async, Int32 timeout, Boolean asyncWrite)    at
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource<code>1
    completion, String methodName, Boolean sendToPipe, Int32 timeout,
    Boolean asyncWrite)    at
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery()    at
    System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.&lt;NonQuery&gt;b__0(DbCommand
    t, DbCommandInterceptionContext</code>1 c)    at
    System.Data.Entity.Infrastructure.Interception.InternalDispatcher<code>1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget
    target, Func</code>3 operation, TInterceptionContext interceptionContext,
    Action<code>3 executing, Action</code>3 executed)    at
    System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand
    command, DbCommandInterceptionContext interceptionContext)    at
    System.Data.Entity.SqlServer.SqlProviderServices.&lt;>c__DisplayClass1a.b__19(DbConnection
    conn)    at
    System.Data.Entity.SqlServer.SqlProviderServices.&lt;>c__DisplayClass33.b__32()
    at
    System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.&lt;>c__DisplayClass1.b__0()
    at
    System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func<code>1
    operation)    at
    System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action
    operation)    at
    System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection
    sqlConnection, Action</code>1 act)    at
    System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection
    sqlConnection, Action<code>1 act)    at
    System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable</code>1
    commandTimeout, DbConnection sqlConnection, String
    createDatabaseScript)    at
    System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection
    connection, Nullable<code>1 commandTimeout, StoreItemCollection
    storeItemCollection)    at
    System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection
    connection, Nullable</code>1 commandTimeout, StoreItemCollection
    storeItemCollection)    at
    System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()    at
    System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection
    connection)    at
    System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action
    mustSucceedToKeepDatabase)    at
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action
    mustSucceedToKeepDatabase)    at
    System.Data.Entity.Migrations.DbMigrator.Update(String
    targetMigration)    at
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String
    targetMigration)    at
    System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() 
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate
    callBackDelegate)    at
    System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)<br>
    at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner
    runner)    at
    System.Data.Entity.Migrations.Design.ToolingFacade.Update(String
    targetMigration, Boolean force)    at
    System.Data.Entity.Migrations.UpdateDatabaseCommand.&lt;>c__DisplayClass2.&lt;.ctor>b__0()
    at
    System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action
    command) ClientConnectionId:23ca49c1-4797-4bc3-8f16-f34fd77f2cbe A
    file activation error occurred. The physical file name '\baza.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. PM></p>
  </blockquote>