The specified table does not exist. [__MigrationHistory]

I used the following method as the Entity Framework Code First became available:

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

Recently, I noticed that when I hit dbContext.Database.Create() I get the following exception:

Fixed System.Data.SqlServerCe.SqlCeException

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

If I go to "Debugging - Exceptions" and mark "Thrown" for "Excluding the total runtime of the language", this will stop the execution and I will get the above exception. If I remove it, the database seems to be created correctly, but I get four repetitions of the following error statements in the output window:

The first random error like "System.Data.SqlServerCe.SqlCeException" occurred in System.Data.SqlServerCe.dll. The first exception of the type "System.Data.SqlServerCe.SqlCeException" appeared in System.Data.SqlServerCe in System.Data.SqlServerCe. Entity.dll the first random error of type "System.Data.SqlServerCe.SqlCeException" occurred in System.Data.Entity.dll the first random error of type "System.Data.EntityCommandExecutionException" occurred in System.Data.Entity.dll the first random error of type "System.Data.EntityCommandExecutionException" The first exception of type "System.Data.EntityCommandExecutionException" has appeared in System.Data.Entity.dll

Placing a try / catch block around dbContext.Database.Create() has no effect.

My goal is to create a completely empty database and then manually populate it with data. I do not want to use the new Migrations feature for the Entity Framework.

What can I do to eliminate the first random exceptions?

+4
source share
1 answer

My goal is to create a completely empty database

Suppose you mean a database with the actual schema objects required by your model?

If so, you have two options:

1) Ignore exceptions. Using SqlCE, EF uses the Migrations pipeline internally when creating databases. Exceptions are part of the implementation of how Migrations determines the existence of the __MigrationHistory table.

2) Use outdated APIs. The database creation APIs in ObjectContext use an obsolete non-migration path. Pass your DbContext to the IObjectContextAdapter to get the ObjectContext link.

+5
source

All Articles