gpt4 book ai didi

entity-framework - 指定的表不存在。 [ __MigrationHistory ]

转载 作者:行者123 更新时间:2023-12-04 08:32:22 26 4
gpt4 key购买 nike

自从 Entity Framework Code First 可用以来,我一直在使用以下方法:

public virtual void CreateDatabase()
{
var dbContext = _dbContextLocator.Current;
dbContext.Database.Delete();
dbContext.Database.Create();
dbContext.Database.Initialize(true);
}

最近,我注意到当 dbContext.Database.Create()被击中,我得到以下异常:

System.Data.SqlServerCe.SqlCeException occurred
Message=The specified table does not exist. [ __MigrationHistory ]
Source=SQL Server Compact ADO.NET Data Provider
ErrorCode=-2147467259
HResult=-2147217865
NativeError=0
StackTrace: at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
InnerException:



如果我去调试 - 异常并检查“公共(public)语言运行时异常”的抛出,这会导致执行停止,我得到上述异常。如果我取消选中它,数据库似乎已正确创建,但我在“输出”窗口中得到以下错误语句的四次重复:

A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.Entity.dll
A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll
A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll
A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll



dbContext.Database.Create() 周围放置一个 try/catch block 没有效果。

我的目标是创建一个完全空白的数据库,然后手动填充数据。我不想使用 Entity Framework 的新迁移功能。

我能做些什么来消除第一次机会异常?

最佳答案

My goal is to create a completely blank database



假设您的意思是具有模型所需的实际模式对象的数据库?

如果是这样,您有两个选择:

1) 忽略异常 - 使用 SqlCE,EF 在创建数据库时在内部使用迁移管道。异常(exception)是 Migrations 如何确定 __MigrationHistory 表存在的实现细节。

2) 使用遗留 API - ObjectContext 上的数据库创建 API 使用遗留的非迁移代码路径。将您的 DbContext 转换为 IObjectContextAdapter 以获得 ObjectContext 引用。

关于entity-framework - 指定的表不存在。 [ __MigrationHistory ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927311/

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