我正在使用 EF6,并且遇到数据库自动迁移问题。数据库存在,并且数据库中没有架构或数据更改。但由于某种奇怪的原因,应用程序似乎试图在机器重新启动时在数据库中重新创建现有表。并因此导致错误。我的问题是:
- 为什么在没有数据库架构更改的情况下会自动进行数据库迁移?是什么触发了它?
- 如何修复它。
我使用 Include 方法来加载这样的实体,当我调用它时出现错误:
using (var context = new MyDataContext())
{
return context.Students.Include("Courses");
}
In the Student
我的类有这个导航属性:
public class Student
{
...
public virtual ICollection<Course> Courses { get; set; }
}
在Configuration.cs中:
public Configuration()
{
this.AutomaticMigrationsEnabled = true;
this.AutomaticMigrationDataLossAllowed = true;
}
...
我还设置了自动数据库迁移MyDataContext
像这样的类:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDataContext, MyProject.DataModel.Migrations.Configuration>());
目前的问题是,99% 的情况下,一切正常,但由于某种未知的原因,会出现以下错误:
System.Data.SqlClient.SqlException(0x80131904):数据库中已有一个名为“Courses”的对象。
在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,操作1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 包裹CloseInAction)
在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔callerHasConnectionLock,布尔asyncClose)
在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSetbulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady)
在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串方法名称,布尔异步,Int32 超时,布尔 asyncWrite)
在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource)1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext
1 c)
在 System.Data.Entity.Infrastruct.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func
3. 操作、TInterceptionContext拦截上下文、Action3 executing, Action
3 执行)
在System.Data.Entity.Infrastruct.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext拦截Context)
在 System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
在System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction事务,MigrationStatement迁移语句,DbInterceptionContext拦截Context)
在 System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable
1个migrationStatements、DbConnection连接)
在 System.Data.Entity.Migrations.DbMigrator.c__DisplayClass30.b__2e()
在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.c__DisplayClass1.b__0()
在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
1个migrationStatements、DbTransaction(现有事务)
在 System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable
1 操作,IEnumerable1 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
1个pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
在 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(字符串 targetMigration)
在 System.Data.Entity.Migrations.DbMigrator.c__DisplayClassc.b__b()
在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作必须成功保持数据库)
在 System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration)
在 System.Data.Entity.MigrateDatabaseToLatestVersion2.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf
1.b__e()
在 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(操作操作)
在 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
在 System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)
在 System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
1 个动作)
在 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型实体类型)
在 System.Data.Entity.Internal.Linq.InternalSet1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet
1.包含(字符串路径)
在 System.Data.Entity.Infrastruct.DbQuery`1.Include(字符串路径)
任何帮助表示赞赏!
谢谢你!!