pencilCake
pencilCake

Reputation: 53223

How do I create new entities based on old entities in Entity Framework?

I need to

  1. load some entities into memory,

  2. change a couple of its 'scalar'

  3. property values -without touching its Navigation Properties. and

  4. SAVE them as a NEW Entity into the Db.

    How can I achieve this in EntityFramework, is it enough to set the object's ID to NULL to save it as a new entity or what should be the trick?

Upvotes: 4

Views: 2475

Answers (4)

Aaron Newton
Aaron Newton

Reputation: 2306

You could clone the object with Automapper, modify a few of the properties and save this as a new entity.

For example:

//Get the source object from a fictitious repository
Person source = PersonRepository.GetPersonByID(555);

//Create an AutoMapper Mapper
Mapper.CreateMap<Person, Person>();
//If you want to ignore the primary key of the original object, use something like this:
//Mapper.CreateMap<Person, Person>().ForMember(x => x.PersonID, y => y.Ignore());

//Clone your source person
var clone = Mapper.Map<Person>(source);

//Set some property
clone.SomeProperty = 123;

//Save our clone against the fictional repository
PersonRepository.Save(clone);

//You could also return your clone at this point...

I used this approach the other day to clone records. One handy thing you can do is take the source identified, e.g. source.PersonID, and store this against clone.ParentID so you could find the origin of the clone (you can go ahead and foreign key this if you want).

Source/Suggested reading - Copy object to object (with Automapper ?)

You could also map to a new entity type if required - see the Automapper wiki - https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

Upvotes: 1

Pop Catalin
Pop Catalin

Reputation: 62920

You simply need to change the entity state from Modified to Added and Entity Framework will perform an insert instead of an update.

Example using DbContext API:

DbContext context = // ...
foreach (var entityEntry in context.ChangeTracker.Entries())
{
    if (entityEntry.State == EntityState.Modified)
         entityEntry.State = EntityState.Added;
}
ct.SaveChanges();

Upvotes: 2

CodeWarrior
CodeWarrior

Reputation: 7470

I had this same difficulty in a project where entities must be unique and changes to an entity would spawn a new entity. I created a Clone method in the entity class which takes the entity, and returns the same type. It creates a new instance, copies all of the relevant properties, then returns the new entity.

To take this further, you could likely create a base class with a Clone method that uses reflection to copy property values and have your entities inherit from it.

Upvotes: 0

Steven M
Steven M

Reputation: 594

Depending on how you're using Entity Framework, I believe you can detach and attach your entity, change the scalar values (including the ID) and mark it as "Added". You may still have to connect/add the foreign entities. See this page for a bit more info on what I'm talking about:

http://msdn.microsoft.com/en-us/data/jj592676.aspx

Upvotes: 0

Related Questions