Copy records from two databases using EF

I need to copy data from one database to another using EF. For instance. I have the following table relationships: Forms-> FormVersions-> FormLayouts ... We have different forms in both databases, and we want to collect them into one database. Basically, I want to recursively load a Form object from one database and save it to another database with all its links. I also need to change the identifiers of the object and related objects, if there are objects with the same ID in the second database.

So far, I have the 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 get an error: "The EntityKey property can only be set when the current value of the property is null."

Will you help with the implementation?

+3
source share
3

- ( ) . :

  • context.Detach(form)
  • EntityKey null
  • context1.AddObject(form)
+4

E.J. . , Entity Framework, , , . Include, , select. , , .

http://msdn.microsoft.com/en-us/library/bb738708.aspx

, . , , , , .

, , , EntityGraph. , , . .

RIA Services Silverlight, , .Net.

http://riaservicescontrib.codeplex.com/wikipage?title=EntityGraphs

Edit1: EntityGraph , DetachEntityGraph RIA-, , , .

Edit2: Alex Jame - . - notracking. , , .

Entity Framework -

+3

If you make only a few records, the Ladislav clause will probably work, but if you are moving a lot of data, you should / might consider making this movement in a stored procedure. The entire operation can be performed on the server, without having to move objects from the db server, to your interface and vice versa. One call to SP will do all this.

Performance will be much better, which may or may not matter in your case.

+1
source

All Articles