c# - How to copy an entity from one Entity Framework context to another? -


How to copy a reference from a reference (inherited from DbContext )

Everything that I only worked for object contact , but is not for DbContext or DbContext Uses but does not work

  • Uses an object contact: CodeHelp Clonholder on project
  • Setting up the lazy loading; Enabled incorrect results in filling the ICollection;
  • Setting Proxy Credentials ICollection & lt;> false results have been enabled in keeping properties as null (foreign key)
  • Insertion & lt;> State = i.e.LoCoLocation> do not set assets (alien-keys) before adding properties, or blocking the id set later on.
  • Asnotricining () results exceptions (one of the following, it depends on the remote context of the parents and the eligibility of the first one; t before the property or the parent):
    • Parent: The object in the role 'ModelFirstSub_First_Target' can not be automatically added to the context because it was recovered using the Notrecking merge option. Object: The object could not be linked or not attached because its EntityReference has an EntityKey property value that does not match EntityKey for this object.

I will use it for two purposes:

  1. Copy from one database to another (the destination will be a clone Originally, a local copy of a remote database should be protected.
  2. Add or update selected institutions from one database to another (Upstreaming changes in local cache in remote origin) No need to maintain the newly created object ID.

How to do this?

EF 6.1.1, .NET 4.5.2, C #

Here is a test code that attempts to simulate the second operation (changes back up from the remote database) Does:

  var added first position = local kontex Fasts. Nantraking () Single (M => m.Id == 4); Var updatedFirst = localContext.Firsts.AsNoTracking (). Single (M = & gt; m.Id == 2); Added first ITEM First (). Id = 0; Added first First. first (). First id = 0; Added First.Id = 0; RemoteContext.FirstItems.Add (addedFirst.Items.First ()); RemoteContext.Firsts.Add (addedFirst); Var Basic First = Remote Contains.Firsts Single (M = & gt; MIID == 2); Var original item = original First.Items.First (); OriginalItem.Title = Updated first. ITEM First (). Title; Model here:  
  Public class ModelFirstBase {Public Virtual Ent ID (get; Set;} Public Virtual I {Receive; Set;} Public Virtual String Title {get; set;}} Public Square ModelFirstSub {Public Virtual Ent ID (Get; Set;} Public Virtual Integer FirstId {get; set;} Public Virtual Model FirstBase {Received first; Set;} public virtual string title {get} set;}}  

PS: The properties should be kept virtual because the model is shared with the production app, which is useful with dynamic proxy.

Did you try to translate the model into just another model:

  public class ModelFirstBase {public virtual id id (get; set;} public Virtual Ilectection & lt; ModelFirstSub & gt; Item {Receive; Set;} Public Virtual string Title {get; set;}} public class ModelFirstSub {public virtual Inti ID (get; Set; } Public Virtual Integer FirstId {get; Set; } Received {first; public virtual model FirstBase; Set; } Public Virtual String Title {get; Set; }} Public Class ModelTranslator {Public ModelFirstSub TranslationModelFirstBase Unit} {some error handling and blank checks are var second = new ModelFirstSub () {first ID = institution.ID, .....}; Second return; }} Public Zero TransformModals () {// I have not thought about settling the content, you should Var firstContext = New FirstContext (); Var secondContext = new seconds contact (); Forex Currency (for the first time in firstContext.ModelFirstBases) {var second = New ModelTranslator () TranslationModel Firstbase (First); SecondContext.ModelFirstSubs.Add (second); } SecondContext.SaveChanges (); }  

However note that bulk of EF data is not for duplication, even if it is not the actual solution to work if you use some equivalent of SQL Server or some DB If you use, you should think of SQLBulkPP instead.

SQLBulkCopy:


Comments