title: How to avoid recreate an existing database using automatic Code First Migration
body: <p>I am using EF6, and having an issue with database automatic migration. The database exists, and there is no schema nor data changes in the db. But for some strange reason, the application seems to trying to re-create an existing table in the db on machine reboot. And therefore cause errors. My questions are:</p>
  
  <ol>
  <li>Why there will be automatic db migration when there is no db schema change? What triggered it?</li>
  <li>How to fix it.</li>
  </ol>
  
  <p>I use Include method to load entities like this, and the error occurs when I call it:</p>
  
  <pre><code>using (var context = new MyDataContext())
  {
      return context.Students.Include("Courses");
  }
  </code></pre>
  
  <p>In the <code>Student</code> class I have this navigation property:</p>
  
  <pre><code>public class Student
  {    
  ...
  
     public virtual ICollection&lt;Course&gt; Courses { get; set; } 
  }
  </code></pre>
  
  <p>In the Configuration.cs:</p>
  
  <pre><code>public Configuration()
  {
     this.AutomaticMigrationsEnabled = true;
     this.AutomaticMigrationDataLossAllowed = true;
  }
  ...
  </code></pre>
  
  <p>I have also set the automatic database migration in the <code>MyDataContext</code> class like this:</p>
  
  <pre><code>Database.SetInitializer(new MigrateDatabaseToLatestVersion&lt;MyDataContext, MyProject.DataModel.Migrations.Configuration&gt;());
  </code></pre>
  
  <p>Currently, the issue is, 99% of the time, everything works just fine, and for some unknown reason, the following error occurs:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Courses' in the database.
       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.Internal.InterceptableDbCommand.ExecuteNonQuery()
       at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
       at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable<code>1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
       at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable</code>1 migrationStatements, DbConnection connection)
       at System.Data.Entity.Migrations.DbMigrator.&lt;>c__DisplayClass30.b__2e()
       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.Migrations.DbMigrator.ExecuteStatements(IEnumerable</code>1 migrationStatements, DbTransaction existingTransaction)
       at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable<code>1 migrationStatements)
       at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable</code>1 operations, IEnumerable<code>1 systemOperations, Boolean downgrading, Boolean auto)
       at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading)
       at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
       at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable</code>1 pendingMigrations, String targetMigrationId, String lastMigrationId)
       at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
       at System.Data.Entity.Migrations.DbMigrator.&lt;>c__DisplayClassc.b__b()
       at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
       at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
       at System.Data.Entity.MigrateDatabaseToLatestVersion<code>2.InitializeDatabase(TContext context)
       at System.Data.Entity.Internal.InternalContext.&lt;&gt;c__DisplayClassf</code>1.b__e()
       at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       at System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)
       at System.Data.Entity.Internal.RetryAction<code>1.PerformAction(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action</code>1 action)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet<code>1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet</code>1.Include(String path)
       at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)</p>
  </blockquote>
  
  <p>Any helps are appreciated!
  Thank you!!</p>