3

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");
}
Cybercop
  • 8,475
  • 21
  • 75
  • 135

1 Answers1

0

You should refactor your code and move seed stuff to separate method, at this example - static Seeder.Seed. Then you can simply call it from any place, without efforts, by passing to it instance of your contex. Also, your first piece of code, probably, only run migrations, not calling Seed at all:

public class Seeder
{
     public static void Seed(CUDbContext context)
     {
          //your seed logic...
     }
}

public  class Configuration : DbMigrationsConfiguration<CUDbContext>
{
    //other stuff...    
    protected override void Seed(CUDbContext context)
    {
        Seeder.Seed(context);
    }
}

Controller:

public ActionResult Seed()
{
    using(var context = new CUDbContext())
    {
        Seeder.Seed(context);
    }
    return Content("Ok")
}
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26
  • Inside the seed method of seeder class, whatever logic I have are not being created in database. Tables are created though – Cybercop Nov 12 '17 at 11:57