实体框架种子方法不起作用

时间:2016-08-06 09:08:44

标签: asp.net-mvc entity-framework ef-migrations

我正在使用Code首次迁移方法,并且在填充数据方面存在问题。 正在命令update-database上创建数据库/表,但未插入数据。

这是代码

internal sealed class Configuration : DbMigrationsConfiguration<webapp_sample.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            //ContextKey = "webapp_sample.Models.ApplicationDbContext";
        }

        protected override void Seed(webapp_sample.Models.ApplicationDbContext context)
        {

            context.Companies.AddOrUpdate(p => p.Name,
                new Models.Company { CompanyId = 1, Name = "ABC Traders" },
                new Models.Company { CompanyId = 2, Name = "XYZ Traders" }
                );
            context.Divisions.AddOrUpdate(p => p.Name,
                new Models.Division { CompanyId = 1, DivisionId = 1, Name = "IT" },
                new Models.Division { CompanyId = 1, DivisionId = 2, Name = "Purchasing" },
                new Models.Division { CompanyId = 1, DivisionId = 3, Name = "Finance" },
                new Models.Division { CompanyId = 1, DivisionId = 4, Name = "Production" },
                new Models.Division { CompanyId = 1, DivisionId = 5, Name = "Retail" },
                new Models.Division { CompanyId = 2, DivisionId = 6, Name = "HR" },
                new Models.Division { CompanyId = 2, DivisionId = 7, Name = "Cafe" },
                new Models.Division { CompanyId = 2, DivisionId = 8, Name = "Projects" }
                );
            context.SubDivisions.AddOrUpdate(p => p.Name,
                new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 1, Name = "Application" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 2, Name = " Infra" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 3, Name = "MIS" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 2, SubDivisionId = 1, Name = "Purchasing" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 3, SubDivisionId = 1, Name = "Finance" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 4, SubDivisionId = 1, Name = "Production" },
                new Models.SubDivision { CompanyId = 1, DivisionId = 5, SubDivisionId = 1, Name = "Retail" },
                new Models.SubDivision { CompanyId = 2, DivisionId = 5, SubDivisionId = 1, Name = "Admin" },
                new Models.SubDivision { CompanyId = 2, DivisionId = 6, SubDivisionId = 1, Name = "Cafe" },
                new Models.SubDivision { CompanyId = 2, DivisionId = 7, SubDivisionId = 1, Name = "Projects" }
                );
        }


    }

ApplicationDbContext文件如下

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();



            modelBuilder.Entity<ApplicationUser>().Property(a => a.Latitude).HasPrecision(18, 9);
            modelBuilder.Entity<ApplicationUser>().Property(a => a.Longitude).HasPrecision(18, 9);

            base.OnModelCreating(modelBuilder);

        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }


        public DbSet<Company> Companies { get; set; }

        public DbSet<Division> Divisions { get; set; }


        public DbSet<SubDivision> SubDivisions { get; set; }

        public System.Data.Entity.DbSet<webapp_sample.Models.MainMenu> MainMenus { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

您使用的是EF7吗?

您可以创建一个类:

public class DataInitializer
{
        webapp_sample.Models.ApplicationDbContext _context

        public void InitializeDataAsync(IServiceProvider serviceProvider)
        {
           _context = serviceProvider.GetService<webapp_sample.Models.ApplicationDbContext>();
           // Add the code for inintializing at here
           ...
        }
}

您还需要使用_context.SaveChanges();。

在Startup.cs文件中,您可以执行以下操作:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
            ...
            DataInitializer dataseed = new DataInitializer();
            dataseed.InitializeDataAsync(app.ApplicationServices);
}

希望这有帮助!