I am planning to host my asp.net mvc application. And I wanted my client to be able to run seed method from controller's method.
This is what I have now:
public class Configuration : DbMigrationsConfiguration<CUDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(CUDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//if (context.Database.Exists()) return;
#region Some sample data
context.Persons.AddOrUpdate(
new Person
{
//some information
});
#endregion
public void RunSeed()
{
var context = new CUDbContext();
Seed(context);
}
}
And this is how I am calling seed method from controller:
public ActionResult Seed()
{
var db = new DAL.Migrations.Configuration {ContextType = typeof(CUDbContext)};
var migrator = new DbMigrator(db);
var scriptor = new MigratorScriptingDecorator(migrator);
var script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null).ToString();
//Debug.Write(script);
migrator.Update();
return RedirectToAction("Index");
}
My controller's method is based on this post.
However, the database doesn't get updated when I hit the controller with seed method.
Any suggestion how that works. Thing is client will not have visual studio to go package manager console to run update-database command. So I would like to be able to do that from controller's method.
Also I tried this in controller and it didn't work:
public ActionResult Seed()
{
var db = new DAL.Migrations.Configuration {ContextType = typeof(CUDbContext)};
db.RunSeed();
//var migrator = new DbMigrator(db);
//var scriptor = new MigratorScriptingDecorator(migrator);
//var script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null).ToString();
////Debug.Write(script);
//migrator.Update();
return RedirectToAction("Index");
}