【问题标题】:Run seed method from controller从控制器运行种子方法
【发布时间】:2018-03-20 19:23:23
【问题描述】:

我打算托管我的 asp.net mvc 应用程序。而且我希望我的客户能够从控制器的方法中运行种子方法。

这就是我现在拥有的:

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

这就是我从控制器调用种子方法的方式:

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

我的控制器的方法是基于this 发帖。

但是,当我使用种子方法点击控制器时,数据库没有更新。

任何建议如何工作。问题是客户端将没有 Visual Studio 去包管理器控制台运行 update-database 命令。所以我希望能够从控制器的方法中做到这一点。

我也在控制器中尝试过,但没有成功:

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc entity-framework


    【解决方案1】:

    您应该重构您的代码并将种子内容移至单独的方法,在此示例中 - 静态 Seeder.Seed。然后,您可以通过将您的上下文实例传递给它,轻松地从任何地方调用它。此外,您的第一段代码可能只运行迁移,根本不调用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);
        }
    }
    

    控制器:

    public ActionResult Seed()
    {
        using(var context = new CUDbContext())
        {
            Seeder.Seed(context);
        }
        return Content("Ok")
    }
    

    【讨论】:

    • 在种子类的种子方法中,我所拥有的任何逻辑都不会在数据库中创建。虽然创建了表
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2017-10-14
    • 2014-05-17
    相关资源
    最近更新 更多