使用MVC5.2中的EntityFramework 6.1.1从现有数据库初始化Code​​-First实体

时间:2014-10-29 21:41:19

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

我在MVC5.2项目中安装了EF6.1.1(使用VS2013)。我成功地使用代码优先生成数据库,我还编写了一个带有虚拟数据的初始化程序,用于测试。对于生产,我需要从不同服务器上的旧数据库加载初始数据。表结构相似但不完全相同,并且表在两个数据库中具有相同的名称;我不能为两者使用相同的模型。

除了代码优先上下文之外,我已经为旧数据库声明了一个上下文,但我不知道如何处理它。理想情况下,我想从旧数据库中提取实体,然后按字段方式复制到新实体。如果那是不可能的,我想拉出旧数据行,并以某种方式用数据填充新实体。

这是代码优先数据库的上下文声明:

namespace ITDAccounting.DAL
{
    public class ITDAccountingContext : DbContext
    {
        public DbSet<Department> Departments { get; set; }
        public DbSet<Employee> Employees { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
     }
}

我对旧数据库的上下文:

namespace ITDAccounting.DAL
{
    public class BillableUnitsContext : DbContext
    {
// 
    }
}

我的一些初始化代码。显然,这是虚假的数据;我想用从现有数据库中提取合法条目的代码替换它:

namespace ITDAccounting.DAL
{
    public class ITDAccountingInitializer : DropCreateDatabaseAlways<ITDAccountingContext>
    {
        BillableUnitsContext existingDataContext = new BillableUnitsContext();

        protected override void Seed(ITDAccountingContext context)
        {    
            var employees = new List<Employee>
            {
                new Employee{EffDate=DateTime.Parse("2014-07-01"),Status="A", Number="999001", Wages=96000.00M, Benefits=25000.00M,  Communications=0.0M, Tools=60.0M, TradeMemberships=0.0M, Training=160.0M}
                ,new Employee{EffDate=DateTime.Parse("2014-07-02"),Status="A", Number="999002", Wages=86000.00M, Benefits=25001.00M,  Communications=300.0M, Tools=50.0M, TradeMemberships=10.0M, Training=150.0M}
                ,new Employee{EffDate=DateTime.Parse("2014-07-03"),Status="A", Number="999003", Wages=76000.00M, Benefits=25002.00M,  Communications=40.0M, Tools=40.0M, TradeMemberships=10.0M, Training=140.0M}
                ,new Employee{EffDate=DateTime.Parse("2014-07-04"),Status="A", Number="999004", Wages=66000.00M, Benefits=25003.00M,  Communications=50.0M, Tools=30.0M, TradeMemberships=10.0M, Training=130.0M}
                ,new Employee{EffDate=DateTime.Parse("2014-07-05"),Status="A", Number="999005", Wages=56000.00M, Benefits=25004.00M,  Communications=60.0M, Tools=20.0M, TradeMemberships=10.0M, Training=120.0M}
                ,new Employee{EffDate=DateTime.Parse("2014-07-06"),Status="A", Number="999006", Wages=46000.00M, Benefits=25005.00M,  Communications=750.0M, Tools=10.0M, TradeMemberships=10.0M, Training=110.0M}
             };
        employees.ForEach(e => context.Employees.Add(e));
        context.SaveChanges();

        var departments = new List<Department>
        {
            new Department{ EffDate=DateTime.Parse("2014-07-01"), Status="A", Name="Tacos", Number="999765", Fund="7777", BalanceSheetAccount="44445555", RevenueAccount="11113434"}
            ,new Department{ EffDate=DateTime.Parse("2014-07-02"), Status="A", Name="Burritos", Number="999234", Fund="9080", BalanceSheetAccount="44441111", RevenueAccount="11114545"}
            ,new Department{ EffDate=DateTime.Parse("2014-07-31"), Status="A", Name="BirthdayCake", Number="998754", Fund="1040", BalanceSheetAccount="44442222", RevenueAccount="11116466"}
            ,new Department{ EffDate=DateTime.Parse("2014-07-04"), Status="A", Name="Diet Creme Soda", Number="991234", Fund="0012", BalanceSheetAccount="44443333", RevenueAccount="11112512"}

        };
        departments.ForEach(d => context.Departments.Add(d));
        context.SaveChanges();
    }
}

提前感谢任何指导。

2 个答案:

答案 0 :(得分:0)

这样的事情:

    protected override void Seed(ITDAccountingContext context)
    {
        using (BillableUnitsContext existingDataContext = new BillableUnitsContext())
        {
            var newEmployeeRecords = new List<Employee>();
            foreach (var oldDbEmployee in existingDataContext.Employees.where(e=>MeetsYourSelectionCriteria(e)) 
            {
                newEmployeeRecords.Add(
                    new Employee 
                    {
                        Name = oldDbEmployee.Name, 
                        Rank = oldDbEmployee.Rank, 
                        SerialNo=oldDbEmployee.SerialNo
                    });   
            }

            //similar stuff for departments

        }
        context.Employees.AddRange(newEmployeeRecords);
        context.SaveChanges();
    }

答案 1 :(得分:0)

老实说,你如何做到这一点取决于很多因素。 EF通常不是ETL工作的好选择。您插入的每一行都会在本地数据缓存中创建越来越大的内存占用,并且需要很长时间。 EF不适用于批量操作。

您应该使用SSIS或其他专用ETL工具。