如何使用EntityFramework在两个数据库之间进行数据迁移 [英] How to do data migration between two databases using EntityFramework

查看:212
本文介绍了如何使用EntityFramework在两个数据库之间进行数据迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Entity Framework将数据从一个数据库传输到另一个类似的数据库(数据库之间的区别在于某些表只有不同的列名)。这是我第一次遇到这个,所以可以有人
给我一些提示并指导如何处理这个问题。我创建了实体数据模型,但不知道如何从这一点继续。


var oldDb = new oldDBContext();


var newDb = new newDBContext();


因此需要在newDb中移动oldDB中的信息。  那么我如何映射信息,虽然在不同的表中更改了某些列的名称? 


我很抱歉这些错误的解释和基本的东西,我是只是新事物并没有找到好的解决方案。

解决方案

你好 Kristian Kanev


根据您的描述,似乎您想创建一个新的DbContext并使用不同的列名映射实体类(如果是这种情况)。我建议您可以使用Fluent API来实现它,请在您上面添加如下代码:newDbcontext
class:

 modelBuilder.Entity< Department> ;()
.Property(t => t.Name)
.HasColumnName(" DepartmentName");

有关详细信息,请参阅(请检查短语:将CLR属性映射到数据库中的特定列):


https://msdn.microsoft.com/en- us / library / jj591617(v = vs.113).aspx


祝你好运,


Cole Wu


I need to use Entity Framework in order to transfer data from one database to another similar database (the difference between the databases are that some of the tables just have different column names). This is my first encounter with this, so can someone give me some tips and guide how to deal with this problem. I have created Entity Data Models but don't know how to continue from this point.

var oldDb = new oldDBContext();

var newDb = new newDBContext();

So the information from the oldDB need to be moved in the newDb.  So how I can map the information although the fact that some column's names are changed in the different tables? 

I am sorry for the bad explanation and the basic stuff, I am just new in this things and couldn't find good solutions.

解决方案

Hi Kristian Kanev,

According to your description, it seems that you want to create a new DbContext and map the entity class with different column name, if it is the case. I would suggest that you could use Fluent API to achieve it, please add the code like below on you newDbcontext class:

modelBuilder.Entity<Department>() 
    .Property(t => t.Name) 
    .HasColumnName("DepartmentName"); 

For more information, please refer to (please check the phrase: Mapping a CLR Property to a Specific Column in the Database):

https://msdn.microsoft.com/en-us/library/jj591617(v=vs.113).aspx

Best regards,

Cole Wu


这篇关于如何使用EntityFramework在两个数据库之间进行数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆