EF Code First: How to delete and recreate database file.
Posted: Tue 13 Mar 2012 21:28
As part of unit tests, I would like to delete the SQLite data file, and have it recreated. I'm using EF Code First, with DotConnect for SQLite. I'm using an SQLiteConnectionFactory like the one you find here: http://www.devart.com/blogs/dotconnect/ ... qlite.html
I'm using the DropCreateDatabaseIfModelChanges initializer for the database.
Some simple example code looks like this:
I clear all pools so that the database file lock will be released. I then delete the database file, and start over. Unfortunately, I get an exception on the second "context.SaveChanges()":
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
Devart.Data.SQLite.SQLiteException: SQLite error\r\nno such table: Items
It correctly recreates the database file, but adds no schema. Why?
Thanks for your help!
Eric
I'm using the DropCreateDatabaseIfModelChanges initializer for the database.
Some simple example code looks like this:
Code: Select all
using (var context = new MyDbContext())
{
var item = context.Items.Create();
item.Name = "Yo";
context.Items.Add(item);
context.SaveChanges();
}
SQLiteConnection.ClearAllPools(true);
File.Delete("MyDbContext.db");
using (var context = new MyDbContext())
{
var item = context.Items.Create();
item.Name = "Yo";
context.Items.Add(item);
context.SaveChanges();
}
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
Devart.Data.SQLite.SQLiteException: SQLite error\r\nno such table: Items
It correctly recreates the database file, but adds no schema. Why?
Thanks for your help!
Eric