Duplicating Entity Framework entities

c# entity-framework-6

Question

I have this EF query:

var records = mydata.Where(a => some condition).ToList();

Records is a list of Record objects. Record is a database table that has a one to many relationship called "relation".

The EF object represents that member variable as Collection<Relation>.

After the where above, I see that "relation" member variable contains a collection of 18 entities.

I need to duplicate those Record objects, so that I am detaching them first, this way:

var detached = this.DetachEntities(records, db, "RecordId");

Where this.DetachEntities is defined this way:

    private T DetachEntity<T>(T entity, Repositories.GestionActivosEntities db, string keyName) where T : class
    {
        db.Entry(entity).State = EntityState.Detached;

        if (entity.GetType().GetProperty(keyName) != null)
        {
            entity.GetType().GetProperty(keyName).SetValue(entity, 0);
        }

        return entity;
    }

    private List<T> DetachEntities<T>(List<T> entities, Repositories.GestionActivosEntities db, string keyName) where T : class
    {
        foreach (var entity in entities)
        {
            this.DetachEntity(entity, db, keyName);
        }

        return entities;
    }

After I detached the entities, I am doing:

db.Record.AddRange(detached);

The problem, is that, after I detached the list, the relation is not copied also, resulting that "relation" member variable contains no elements.

How can I keep the relation elements after detaching?

EDIT:

This shows the debugging session:

2 instructions 2 show the problem

The breakpoint is placed in the line where the query is done. The cursor shows the line after the query was executed.

This screenshot shows the child records ValorCampo and Workflow

This screenshot shows the child records ValorCampo and Workflow

This screenshot shows the entity after detaching it. Note the child records are not present in the detached object

This screenshot shows the entity after detaching it. Note the child records are not present in the detached object

As I have told, the problem only is when detaching. Child records are not kept. Database rows remain untouched.

1
0
12/19/2019 11:18:08 AM

Popular Answer

I had the same problem, unfortunately navigation properties are lost after detaching an Item or when entity state is changed to detached.

what you can do is clone the entity one way to do this is : Context.Entry(your_entity).CurrentValues.ToObject();

however this will not clone the navigation properties either

if you fully want to clone an object among with navigation properties the easiest way for me to achieve it was using automapper library for c#

Below is a sample usage:

var config = new MapperConfiguration(cfg => cfg.CreateMap<originalObject, T>());
var mapper = new Mapper(config);
// or
var mapper = config.CreateMapper();

T  clonedObject = mapper.Map<T>(originalObject);

after you clone and detach the original object, you can add

db.Record.AddRange(clonedObject );

and below is a generic extension to do it

public static object Map<T>(this T source)
    {
        var fullName = source.GetType().FullName;
        var sourceType = source.GetType();

        var baseType = ObjectContext.GetObjectType(source.GetType());

        var config = new MapperConfiguration(cfg =>
            cfg.CreateMap(sourceType, baseType));




        var mapper = config.CreateMapper();

        var entity = mapper.Map(source, sourceType, baseType);

        return entity;
}

where you can call it like

var clonedObject = originalObject.Map();

Hope this helps!

0
12/23/2019 2:57:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow