Using serialization to copy objects between two object objects in the Entity Framework

Introduction

I know that this is probably already seen as a crazy question , but I'm looking for the most educated advice and PROVEN recommendations for continuing to copy ALL data (i.e. all entities and relationships) from ObjectContextto the newly created one ObjectContext, maintained in another store. See "Cloning" EntityConnections and ObjectContexts in the Entity Framework to find out how I configure this.


Introduction

I saw Cloning Framework data to the Entity , , but:

  • I am looking for the whole enchilada , i.e. entire graphic object: all entities and relationships. Later I will send for a more plastered choice of which parts of the object graph)

  • According to my updates below, I did not make serialization work, but only in special cases. I feel that this is really not a very difficult task, but it was surprisingly difficult. I definitely need to understand how to make it work.


Step 1

Ok, this is what I have tried so far.

I know that when I use the Detach/ Attachpower arc , the relationship is kaput- and I actually want to save the entire graph of the object.

So I was thinking about loading root objects with the option MergeOption.NoTracking:

var serializer = new DataContractSerializer(typeof(Root));

var query = (ObjectQuery<Root>) sourceContext.Roots
    .Include(d => d.Children.Select(c => c.MoreChildren.Select(r => r.EvenMoreChildren)))
    .Include(d => d.Children.Select(c => c.MoreChildren.Select(r => r.MoreAndMoreChildren)))
    .Include(d => d.Children.Select(c => c.MoreChildren.Select(r => r.Shizzle)));

foreach (var d in query.Execute(MergeOption.NoTracking)) {
    //sourceContext.Detach(d); // not needed
    Print(d);
    using (var ios = new MemoryStream()) {
        serializer.WriteObject(ios, d);
        ios.Seek(0, SeekOrigin.Begin);
        var dd = (Root) serializer.ReadObject(ios);
        //Console.WriteLine(string.Join(",", dd.EntityKey.EntityKeyValues.Select(k => k.Key + "=" + k.Value)));
        targetContext.Roots.AddObject(dd);
    }
}

Given that I load objects as untraceable, I no longer need to call sourceContext.Detach(d).

Print , , ( , ).

@ serializer.WriteObject(ios, d) :

" NoTracking merge, Load , EntityCollection EntityReference ."

( , , , , .)

, NoTracking, , ...


2

, sourceContext.ContextOptions.LazyLoadingEnabled = false , , :

" , EntityReference EntityKey, EntityKey ."

, , sourceContext.Detach(d), NoTracking...


3

EntityKey = null ... :

sourceContext.ContextOptions.LazyLoadingEnabled = false;

foreach (var d in query.Execute(MergeOption.NoTracking)) {
    //sourceContext.Detach(d);
    Print(d);
    d.EntityKey = null;
    using (var ios = new MemoryStream()) {
        serializer.WriteObject(ios, d);
        ios.Seek(0, SeekOrigin.Begin);
        var dd = (Root) serializer.ReadObject(ios);
        if (dd.EntityKey != null) {
            Console.WriteLine("Deserialized EntityKey: {0}", string.Join(",", dd.EntityKey.EntityKeyValues.Select(k => k.Key + "=" + k.Value)));
            dd.EntityKey = null;
        }
        targetContext.Roots.AddObject(dd);
    }
}

, , , "" , .

, , , EF- ???

?!!!!?!: (

+3
1

@khovanskiiªn - . , EF, . , , .

, , , EF SQL, . ETL Extract/Transform/Load, , , .

. , SSIS , .

, Entity Framework . , .

, Entity , EF , / . 1 , , sql-side .

, SSIS (Sql Server Integration Services).

+5

All Articles