Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Problem with saving attached Entity with Entity Framework  #20

Description

@Assassyn

When you load entity, commit the unit of work, after the change attempt on such entity there is no way to attach(reattach) it to context and submit changes to database. I have problem when I was using IEntityWithKey but I suppose that similar problem can happens for POCO type of entities.

Problem could be resolved by adding:
public void Attach(T entity) where T : class
{
//If the entity implementes the IEntityWithKey interface then we should use Context's Attach metho
//instead of the set's Attach. Getting an exception
//"Mapping and metadata information could not be found for EntityType 'System.Data.Objects.DataClasses.IEntityWithKey"
//when using set's Attach.
var entityWithKey = typeof (IEntityWithKey);
if (entityWithKey.IsAssignableFrom(entity.GetType()))
{
_context.Attach((IEntityWithKey)entity);
// new line seem to solve the problem
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
return;
}

        GetObjectSet<T>().Attach(entity);
        //_context.DetectChanges(); //Required for POCO entities
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions