logo
down
shadow

Entity Framework 6 deep copy/clone of an entity with dynamic depth


Entity Framework 6 deep copy/clone of an entity with dynamic depth

By : carl w
Date : November 22 2020, 09:00 AM
should help you out I have found a more generic approach for this problem. For anyone who might encounter a similar problem, here is how I solved it for now:
code :
public Item DeepCloneItem(Item item)
{
    Item itemClone = db.Items.FirstOrDefault(i => i.ItemID == item.ItemID);
    deepClone(itemClone);
    db.SaveChanges();
    return itemClone;
}

private void deepClone(Item itemClone)
{
    foreach (Item child in itemClone.ChildrenItems)
    {
        deepClone(child);
    }
    foreach(Parameter param in itemClone.Parameters)
    {
        db.Entry(param).State = EntityState.Added;
    }
    db.Entry(itemClone).State = EntityState.Added;
}


Share : facebook icon twitter icon
Deep Clone using Entity Framework

Deep Clone using Entity Framework


By : Sakshi Arora
Date : March 29 2020, 07:55 AM
this one helps. Just like how we deep clone any other objects. You need override the clone on source object and provide a implementation by copying each memberwise.
Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the

Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the


By : Chris van der Walt
Date : March 29 2020, 07:55 AM
I wish did fix the issue. It's been a while since I wrote the code below, but I vaguely recall running into the same problem and it was occurring because the role being added was currently being tracked by the context, so attaching the stub has the effect of adding a new role with the same Id.
In the following code, I check the ChangeTracker first and use an existing entry if the role is being tracked.
code :
// add roles that are in dto.Roles, but not in resource.Roles
// use the change tracker entry, or add a stub role
var rolesToAdd = fromDto.Roles.Where(r => !toResource.Roles.Any(role => role.Id == r)).ToList();
var roleEntries = dbContext.ChangeTracker.Entries<Role>();

foreach (var id in rolesToAdd)
{
    var role = roleEntries.Where(e => e.Entity.Id == id).Select(e => e.Entity).FirstOrDefault();

    if (role == null)
    {
        role = new Role { Id = id };
        dbContext.Set<Role>().Attach(role);
    }

    toResource.Roles.Add(role);
}
Clone (deep copy) entity framework code first object within database

Clone (deep copy) entity framework code first object within database


By : Joshua Kim
Date : March 29 2020, 07:55 AM
wish of those help I ended up solving this problem by using the Copyable project by Håvard Stranden.
What is the simplest way to copy a record from one entity to another entity with Entity framework?

What is the simplest way to copy a record from one entity to another entity with Entity framework?


By : MTadder
Date : March 29 2020, 07:55 AM
I wish this help you One way to do this is to have 2 entity classes (one for the transaction table and one for the archive table) and use the AutoMapper to populate the archive object from the transactional object, and call the SaveChanges.
Entity Framework 5 deep copy/clone of an entity

Entity Framework 5 deep copy/clone of an entity


By : Shita Aleme
Date : March 29 2020, 07:55 AM
I hope this helps . I am using Entity Framework 5 (DBContext) and I am trying to find the best way to deep copy an entity (i.e. copy the entity and all related objects) and then save the new entities in the database. How can I do this? I have looked into using extension methods such as CloneHelper but I am not sure if it applies to DBContext. , One cheap easy way of cloning an entity is to do something like this:
code :
var originalEntity = Context.MySet.AsNoTracking()
                             .FirstOrDefault(e => e.Id == 1);
Context.MySet.Add(originalEntity);
Context.SaveChanges();
var originalEntity = Context.MySet.Include("MyProperty")
                            .AsNoTracking()
                            .FirstOrDefault(e => e.Id == 1);
Related Posts Related Posts :
  • How to get all ip address in LAN?
  • send value to controller using html.actionlink
  • Assembly "Microsoft.GeneratedCode" loaded
  • count the number of items in view in asp.net mvc
  • Shortcut Keys for Saving or Editing Record in C#
  • Very tricky/complex text formatting: How can I output a string[][] as a table?
  • Fill multiple textboxes after selecting a data from DropDownList in ASP.NET
  • How to launch an application after another is logged in in C#?
  • Unable to retrieve column values from one of the tables joined in a stored procedure?
  • Directories last file name display only
  • Why do I get a COMException when I try to initialize an Interop.Word.Application?
  • Finding subtotal for column in repeater control
  • dropdownlist item selected throw exception (stack empty)
  • How can I list IIS working processes programmatically?
  • How to access nested object from JSON with Json.NET in C#
  • OpenXml DataValidation set predefined List for columns
  • How to find recurring word groups in text with C#?
  • layout conrols to developp universal apps in windows 8.1 pro
  • Adding strings to a list via for loop xna
  • Using Vertex Arrays with OpenTK
  • Optimizing List<string>
  • C# Local variables
  • How can I merge the results of a group by Linq-to-XML query?
  • How to add Report in ASP.NET Project
  • C# How to display all instances of a loop in a label?
  • C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?
  • Is there any negative effect to setting SqlCommand's CommandTimeout to a high value?
  • C# - TreeView Context Menu Out of Place when using keyboard
  • How to read stored procedure output and return it as list
  • How do you maintain changes made by VisualState triggers after setting another VisualState?
  • Should I be unit testing my bootstrapper and if so how?
  • using .SingleOrDefault() inside .Where() will raise the following exception:- System.NotSupportedException was unhandled
  • C# How would I fill a Form1 Parameter in the same seperate class
  • Access gridview row's data from LinkButton inside that row
  • Translating excel function to c# Math.pow not resulting in same values
  • Arranging Ten Inputted Numbers into Ascending and Descending order
  • AsEnumerable and Query Syntax
  • How can I defensively code against randomly referencing "Table 0" and null values?
  • Changing xaml code position based on screen size
  • Castle Windsor resolve ienumerable and name
  • Drawing multiple sprites with a for loop xna 4.0
  • For loop to Populate Textblocks
  • Visual Studio 2015: Create an Empty Project
  • What are .NET classes to replace the old MSXML2.ServerXMLHTTP?
  • Handle leak in .Net threads
  • Excel add-in ribbon click events bubbling
  • validation rule needed for only numbers to be entered
  • Parsing complex XML, no descendent elements exists when selecting a named element
  • Web Api - IEnumerable with complex type as param is null
  • Email address input validation
  • .Net AND operator Regular Expression using strings in a document
  • Windows phone 8.1 POST x-www-form-urlencoded not working
  • Is there any way to make my C# XMLManager work?
  • ASP.NET 5 Console Application (package) - How to create DBContext from connection string?
  • How to use form's function in other class C#
  • WPF Zebra EPL2 Printing issue
  • C# use reflection to capture exception throw
  • Convert strings in DataRow to double
  • Xml-Serialization is not working
  • HTML Agility Pack can't find classes with trailing spaces
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co