简体   繁体   English

从控制器运行种子方法

[英]Run seed method from controller

I am planning to host my asp.net mvc application. 我打算主持我的asp.net mvc应用程序。 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. 事情是客户端没有visual studio去包管理器控制台运行update-database命令。 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");
}

You should refactor your code and move seed stuff to separate method, at this example - static Seeder.Seed . 您应该重构代码并将种子内容移动到单独的方法,在此示例中 - 静态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: 此外,您的第一段代码可能只运行迁移,而不是完全调用Seed

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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM