I am trying to create a SQLite database using entity framework code first. The application is creating the database for the first time and then seeding the database. I create a SQLiteConnection and then pass the connection to a Context. In the constructor of the Context I call Database.SetInitializer<BloggingContext>(new DropCreateDatabaseAlways<BloggingContext>());
I get an exception that says the DeleteDatabase is not supported by the provider.
The following are the supporting classes:
namespace EntityFrameworkConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var sb = new System.Data.SQLite.SQLiteConnectionStringBuilder();
sb.DataSource = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "test.db");
sb.ForeignKeys = true;
sb.BinaryGUID = true;
sb.DateTimeFormat = SQLiteDateFormats.ISO8601;
sb.DateTimeKind = DateTimeKind.Local;
sb.ToFullPath = true;
var conn = new SQLiteConnection(sb.ConnectionString);
using (var context = new BloggingContext(conn))
{
context.Blogs.Add(new Blog { Name = "Yet Another Blog #1" });
context.SaveChanges();
var blogs = (from b in context.Blogs
orderby b.Name
select b).ToList();
}
}
}
}
using System.Data.Common;
using System.Data.Entity;
namespace EntityFrameworkConsoleApplication1
{
[DbConfigurationType(typeof(MyConfiguration))]
public class BloggingContext : DbContext
{
public BloggingContext(DbConnection c):base(c,true)
{
Database.SetInitializer<BloggingContext>(new DropCreateDatabaseAlways<BloggingContext>());
}
public DbSet<Blog> Blogs { get; set; }
}
}
namespace EntityFrameworkConsoleApplication1
{
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.SQLite;
using System.Data.SQLite.Linq;
using System.Reflection;
namespace EntityFrameworkConsoleApplication1
{
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
Type t = Type.GetType(
"System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6");
FieldInfo fi = t.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static);
SetProviderServices("System.Data.SQLite", (DbProviderServices)fi.GetValue(null));
}
}
}
|