简体   繁体   English

Asp.net MVC种子方法无法在发布时运行

[英]Asp.net MVC Seed Method Doesn't Run on Publishing

When i published my project for the first time, seed method run and insert the data to the tables. 当我第一次发布项目时,将运行seed方法并将数据插入表中。 But when i have changed the seed method with more data, seed method did not work. 但是,当我使用更多数据更改了种子方法时,种子方法无法正常工作。

And: Shoud i set false "AutomaticMigrationsEnabled" and "AutomaticMigrationDataLossAllowed" parameters ? 并且:应该设置错误的“ AutomaticMigrationsEnabled”和“ AutomaticMigrationDataLossAllowed”参数吗?

My Configuration File as Below : 我的配置文件如下:

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

If you are using an AutoMapper you may want to configure it in your Global.asax.cs file. 如果您使用的是AutoMapper,则可能需要在Global.asax.cs文件中进行配置。 I just did it by using the following line: 我只是通过使用以下行来做到这一点:

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

and to set the "AutoMapperConfig" you can use the code here 并设置“ AutoMapperConfig”,您可以在此处使用代码

Your Migration Configuration should look like this : 您的迁移配置应如下所示:

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

You may also need to save changes at the end. 您可能还需要最后保存更改。

    context.SaveChanges();

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

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