Question

I've been using the following method since 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 dbContext.Database.Create() is hit, I get the following exception:

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:

If I go to Debug - Exceptions and check Thrown for "Common Language Runtime Exceptions", this causes execution to stop, and I get the above exception. If I uncheck it, the database seems to get created properly, but I get four repeats of the following error statements in my Output window:

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

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

My goal is to create a completely blank database, then fill it with data manually. I do not wish to use the new Migrations feature of Entity Framework.

What can I do to eliminate the first chance exceptions?

Was it helpful?

Solution

My goal is to create a completely blank database

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

If so, you have two options:

1) Ignore the exceptions - With SqlCE, EF uses the Migrations pipeline internally when creating databases. The exceptions are an implementation detail of how Migrations determines existence of the __MigrationHistory table.

2) Use the legacy APIs - The database creation APIs on ObjectContext use the legacy, non-migrations code path. Cast your DbContext to IObjectContextAdapter to obtain an ObjectContext reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top