使用EF从两个数据库之间复制记录 [英] copy records from between two databases using EF
问题描述
到现在为止,我有以下代码:
Form form = null ;
using(var context = new FormEntities())
{
form =(from f in context.Forms
join fv in context.FormVersions on f.ID equals fv.FormID
其中f.ID == 56
选择f).First();
}
var context1 = new FormEntities(name = FormEntities1);
context1.AddObject(Forms,form);
context1.SaveChanges();
我收到错误:EntityKey属性只能在当前值属性为null。
您可以帮助实现吗?
最简单的解决方案是创建Form(新对象)的副本,并添加新对象。否则你可以尝试:
- 调用
context.Detach(form)
- 设置表单的
EntityKey
为null - 调用
context1.AddObject(form)
I need to copy data from one database to another with EF. E.g. I have the following table relations: Forms->FormVersions->FormLayouts... We have different forms in both databases and we want to collect them to one DB. Basically I want to load Form object recursively from one DB and save it to another DB with all his references. Also I need to change IDs of the object and related objects if there are exists objects with the same ID in the second database.
Until now I have following code:
Form form = null;
using (var context = new FormEntities())
{
form = (from f in context.Forms
join fv in context.FormVersions on f.ID equals fv.FormID
where f.ID == 56
select f).First();
}
var context1 = new FormEntities("name=FormEntities1");
context1.AddObject("Forms", form);
context1.SaveChanges();
I'm receiving the error: "The EntityKey property can only be set when the current value of the property is null."
Can you help with implementation?
The simplest solution would be create copy of your Form (new object) and add that new object. Otherwise you can try:
- Call
context.Detach(form)
- Set form's
EntityKey
to null - Call
context1.AddObject(form)
这篇关于使用EF从两个数据库之间复制记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!