【问题标题】:Asp.net MVC Seed Method Doesn't Run on PublishingAsp.net MVC 种子方法在发布时不运行
【发布时间】:2014-04-01 07:22:32
【问题描述】:

当我第一次发布我的项目时,种子方法运行并将数据插入到表中。但是当我用更多数据更改种子方法时,种子方法不起作用。

并且:我应该设置 false 的“AutomaticMigrationsEnabled”和“AutomaticMigrationDataLossAllowed”参数吗?

我的配置文件如下:

 internal sealed class Configuration : DbMigrationsConfiguration<ModulericaV1.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;

        }

        protected override void Seed(ApplicationDbContext context)
        {
            this.AddUserAndRoles();
        }


        bool AddUserAndRoles()
        {
            bool success = false;

            var idManager = new IdentityManager();
            success = idManager.CreateRole("Admin");
            if (!success == true) return success;

            success = idManager.CreateRole("HR_Admin");
            if (!success == true) return success;

            success = idManager.CreateRole("HR_Visitor");
            if (!success) return success;


            var newUser = new ApplicationUser()
            {
                UserName = "pascal",
                FirstName = "umki",
                LastName = "umkiii",
                Email = "asdfads@asdas.com"
            };

            success = idManager.CreateUser(newUser, "Password1");
            if (!success) return success;

            success = idManager.AddUserToRole(newUser.Id, "Admin");
            if (!success) return success;

            return success;
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc seed


    【解决方案1】:

    如果您使用的是 AutoMapper,您可能需要在 Global.asax.cs 文件中对其进行配置。 我只是使用以下行来做到这一点:

    var autoMapperConfig = new AutoMapperConfig(Assembly.GetExecutingAssembly());
    autoMapperConfig.Execute();
    

    要设置“AutoMapperConfig”,您可以使用代码here

    【讨论】:

      【解决方案2】:

      您的迁移配置应如下所示:

          public Configuration()
          {
              AutomaticMigrationsEnabled = true;
              AutomaticMigrationDataLossAllowed = true;
          }
      

      您可能还需要在最后保存更改。

          context.SaveChanges();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 2018-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多