[英]Initializing Code-First entities from existing database using EntityFramework 6.1.1 in MVC5.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();
}
}
在此先感谢您的指导。
像这样:
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();
}
老实说,您如何执行此操作取决于许多因素。 EF通常不是ETL工作的好选择。 您插入的每一行都会在本地数据缓存中创建越来越大的内存占用空间,并且需要很长时间。 EF不适用于批量操作。
您可能应该使用类似SSIS或其他专用ETL工具的工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.