1

我在 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();
    }
}

提前感谢您的任何指导。

4

2 回答 2

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();
    }
于 2014-10-29T21:59:40.210 回答
0

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

您可能应该使用类似 SSIS 或其他专用 ETL 工具的工具。

于 2014-10-29T23:41:56.450 回答