gpt4 book ai didi

entity-framework-6 - 无效的对象名称 'dbo.__MigrationHistory' 使用 Database.Create;传入连接字符串时的EF6.02

转载 作者:行者123 更新时间:2023-12-03 08:45:05 47 4
gpt4 key购买 nike

尝试使用以下代码创建数据库时遇到错误。
请注意,如果未传入连接字符串,则不会发生此问题。
当我在 IDE 中运行程序时也会出现问题。如果我运行程序 .exe 或在 IDE 中运行单元测试,则不会发生这种情况。

但是,如果数据库是通过运行单元测试或运行 .EXE 创建的,那么 __MigrationHistory表是在主表部分创建的,而不是系统表。

public Context(string connString, bool AddInitialRecords )
: base(connString ?? "MyContextName")
{
this.CheckDatabase(AddInitialRecords);
}

public void CheckDatabase(bool AddInitialRecords)
{
if (this.Database.Exists())
{
// upgrade stuff
}
else
{
Database.Create(); // error occurs here
// seeding stuff
}
}

如果我只使用类似的东西,我就不会遇到问题

var db1 = new Context();
db1.Database.CreateIfNotExists();

我找到了一些文档 here但这让我很困惑。我是从“稳定版本”安装的,我肯定没有经历过 2012 年的事情吗?我对 PM 做错了什么?

该问题的错误消息是....

System.Data.Entity.Core.EntityCommandExecutionException occurred
HResult=-2146232004 Message=An error occurred while executing the command definition. See the inner exception for details.
Source=EntityFramework StackTrace: at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) InnerException: System.Data.SqlClient.SqlException HResult=-2146232060 Message=Invalid object name 'dbo.__MigrationHistory'. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=16 LineNumber=1 Number=208 Procedure="" Server=.\SQLEXPRESS State=1 StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`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& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.b__8() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) InnerException:

最佳答案

发生这种情况是因为 EF 对 __MigrationsHistory 表进行了一些探测。例如,您可以将 EF 与不是使用 EF 迁移创建的现有数据库一起使用,但 EF 无法知道它,因此它会尝试连接到数据库并使用表来检查这一点。如果表不存在,则会抛出异常。然后 EF 捕获异常并做正确的事情(例如,如果需要,创建 __MigrationsHistory 表,或者在不使用迁移的情况下继续)。

通常,在没有调试器的情况下运行时您不会看到此异常。但是在调试代码时 当设置了在抛出异常时中断执行的选项时,您将看到所有正在抛出的异常,即使它们是在内部处理的并且永远不会到达您的代码。默认设置是在抛出异常时不中断,而仅在抛出未处理的异常时才中断。您可以通过选中/取消选中 Debug -> Exceptions 对话框中“Thrown”列中的复选框来更改设置。

在 VS 2017 中,您可以使用 Debug->Windows->Exception Settings 打开异常设置。如果您右键单击“Common Language Runtime Exceptions”,您可以选择“Restore Defaults”,它会在大多数异常被抛出时禁用您的程序。

关于entity-framework-6 - 无效的对象名称 'dbo.__MigrationHistory' 使用 Database.Create;传入连接字符串时的EF6.02,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20877039/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com