Originally I used EF 6 code first approach to create tables in LocalDB (Service-based Database). The code is:
namespace WindowsFormsApp2
{
public class MyDBContext : DbContext
{
public MyDBContext() : base("name=strCon")
{
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));
Database.SetInitializer<MyDBContext>(new DropCreateDatabaseIfModelChanges<MyDBContext>());
//Database.SetInitializer<MyDBContext>(null);
//Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());
}
public DbSet<Assets> Assets { get; set; }
}
}
Connection string in App.config for LocalDB
<connectionStrings>
<add name="strCon" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AppDB.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
</connectionStrings>
My goal is to deploy (publish) on the client machine with the latest model changes without losing the previous data in tables. Database migrations should perform programmatically. But database Initializer DropCreateDatabaseIfModelChanges is DELETE, RECREATING on the database.
Is it possible that EF6 still does not handle such a basic model change automatically without delete, recreate?