简体   繁体   English

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

[英]Initializing Code-First entities from existing database using EntityFramework 6.1.1 in MVC5.2

I have EF6.1.1 installed in a MVC5.2 project (using VS2013). 我在MVC5.2项目中安装了EF6.1.1(使用VS2013)。 I successfully used code-first to generate the database and I also wrote an initializer with dummy data for testing. 我成功地使用代码优先生成了数据库,并且还编写了带有虚拟数据的初始化程序以进行测试。 For production, I need to load the initial data from an older database on a different server. 为了进行生产,我需要从其他服务器上的旧数据库加载初始数据。 The table structures are similar but not identical, and the tables have the same names in both databases; 表结构相似但不完全相同,两个数据库中的表名称相同。 I can't use the same models for both. 我不能为两者使用相同的模型。

In addition to the code-first context I have declared a context for the older database, but I don't know what to do with it. 除了代码优先的上下文,我还声明了旧数据库的上下文,但是我不知道该如何处理。 Ideally, I would like to pull entities from the old database, then copy field-wise to new entities. 理想情况下,我想从旧数据库中提取实体,然后按领域复制到新实体。 If that isn't possible, I'd like to pull rows of old data and somehow populate new entities with the data. 如果这不可能,我想提取旧数据行,并以某种方式用数据填充新实体。

Here is my context declaration for the code-first database: 这是我的代码优先数据库的上下文声明:

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

And my context for the old database: 而我对于旧数据库的上下文:

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

And some of my initializer code. 还有一些我的初始化代码。 Obviously, this is bogus data; 显然,这是伪数据。 I want to replace this with code that pulls legit entries from the existing database: 我想将其替换为从现有数据库中提取合法条目的代码:

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

Thanks in advance for any guidance. 在此先感谢您的指导。

Something like this: 像这样:

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

Honestly, how you do this depends on many factors. 老实说,您如何执行此操作取决于许多因素。 EF is generally not a good choice for ETL work. EF通常不是ETL工作的好选择。 Each row you insert creates a larger and larger memory footprint in the local data cache, and it takes a very long time. 您插入的每一行都会在本地数据缓存中创建越来越大的内存占用空间,并且需要很长时间。 EF is not designed for bulk operations. EF不适用于批量操作。

You should probably use something like SSIS, or other dedicated ETL tools. 您可能应该使用类似SSIS或其他专用ETL工具的工具。

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

相关问题 代码优先:将实体映射到现有数据库表 - Code-first: Mapping entities to existing database tables 从数据库使用EntityFramework C#代码优先-如何在没有主键的情况下映射表 - Using EntityFramework C# code-first from database - how to map table with no primary key 实体框架代码优先使用现有数据库时的好处 - Entity Framework Code-First Benefits when using existing database 在现有MySQL数据库上使用实体框架4.1 Code-First - Using entity framework 4.1 Code-First on existing MySQL database MVC EntityFramework代码优先迁移-Seed()是否由于AmbiguousMatchException而失败? - MVC EntityFramework Code-First Migrations - Seed() Failing with AmbiguousMatchException? MVC EntityFramework代码优先迁移正在执行,但是种子未生效? - MVC EntityFramework Code-First Migration is executing, but Seed not taking effect? MVC EntityFramework代码优先迁移-Seed()是否由于DbUpdateException而失败? - MVC EntityFramework Code-First Migrations - Seed() Failing with DbUpdateException? 使用EntityFramework,代码优先(CTP5),如何创建数据库? - Using EntityFramework, code-first (CTP5), how do you create the database? 实体框架优先代码和现有数据库 - Entity Framework code-first and existing database 首先更新Entity Framework 6中的现有实体 - Updating existing entities in Entity Framework 6 code-first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM