[英]Migrating data from 2 different SQL Server databases using console app
I have 2 web applications which are let's say Legacy1
and New1
. 我有2个Web应用程序,分别是Legacy1
和New1
。 I have separate DbContext
for both applications and separate databases. 我对应用程序和数据库都有单独的DbContext
。
Now I want to migrate data from the Legacy1
database to the New1
database. 现在,我想将数据从Legacy1
数据库迁移到New1
数据库。 I cannot simply use copy database
or export database
options in SQL Server. 我不能在SQL Server中简单地使用copy database
或export database
选项。 Entities are different and I need to incorporate some logic while migrating data. 实体是不同的,在迁移数据时我需要合并一些逻辑。
I have decided to use .Net Core Console (.Net Framework) project to do this. 我决定使用.Net Core Console(.Net Framework)项目来执行此操作。 Do I have to add both DbContexts
to this project? 我是否必须将两个DbContexts
添加到该项目? Also all the entities which I want to map and do the migration or is there any other way to achieve this. 另外,我要映射并进行迁移的所有实体,或者是否有其他方法可以实现此目的。
Finally i found a simple approach to achieve that. 最终,我找到了一种简单的方法来实现这一目标。 I used EF DB First
approach to generate edmx
files for source
and target
databases. 我使用EF DB First
方法为source
数据库和target
数据库生成edmx
文件。 Which gives me access to corresponding Entities
as well. 这也使我可以访问相应的Entities
。
I Queried source database using source dbcontext
and manipulated data as per the requirements and inserted into target database using target dbcontext
. 我使用源dbcontext
源数据库,并根据要求处理数据,然后使用目标dbcontext
将其插入目标数据库。 I used AutoMapper
to map source
entities to Target
entities. 我使用AutoMapper
将source
实体映射到Target
实体。
It was much simpler than i thought earlier. 这比我以前想象的要简单得多。 Hope it helps others having similar scenario. 希望它可以帮助其他有类似情况的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.