-
-
Notifications
You must be signed in to change notification settings - Fork 122
Closed
Labels
Description
hi i want use SqliteDropCreateDatabaseWhenModelChanges this is my codes:
app.Config:
<connectionStrings>
<add name="default" connectionString="data source=app.db;" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
and this is my dbContext class:
public class TestDbContext : DbContext
{
public virtual DbSet<User> Users { get; set; }
public TestDbContext()
: base("default")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<TestDbContext>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
}
}
and last step Main.cs:
var db = new TestDbContext();
var users = db.Users.ToList();
with this code i can create database, but when i use SqliteDropCreateDatabaseWhenModelChanges like this i get error:
public class TestDbContext : DbContext
{
public virtual DbSet<User> Users { get; set; }
public TestDbContext()
: base("default")
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new MyDbContextInitializer(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
}
public class MyDbContextInitializer : SqliteDropCreateDatabaseWhenModelChanges<TestDbContext>
{
public MyDbContextInitializer(DbModelBuilder modelBuilder)
: base(modelBuilder) { }
protected override void Seed(TestDbContext context)
{
context.Users.Add(new User
{
Name = "Richard Niemand",
Created = DateTime.Now
});
base.Seed(context);
}
}
}
Error:
System.MissingMethodException: 'Method not found: 'Void System.Data.Entity.DbModelBuilder.RegisterEntityType(System.Type)'.'