I have a generic repository that is using Entity Framework 4 with the DbContext api. When Update is called, the repository registers the entity and the repository with the unit of work object.
When commit is called on the unit of work, and the entity updated is being tracked by EF (normally it is not, as the repository is for ASP MVC and its model binding technology) the type is a proxy type and the an exception is thrown as the type does not exist in the model. The PersistUpdateOf then falls over when the entity is tracked by EF because EF uses proxy classes. The attached image shows this.

Is there a work around that does not involve disabling proxies that will work with entities that are tracked or untracked.
public void Update<TEntity>(TEntity entity) where TEntity : EntityBase
{
//Audit the update
if (entity is IAuditable)
{
(entity as IAuditable).AuditEntityUpdate();
}
//Register the entity as Amended with the unit of work
_unitOfWork.RegisterAmended(entity, this);
}
public virtual void PersistUpdateOf(EntityBase entity)
{
_context.Set(entity.GetType()).Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
}
Just remove the line that you
Attachthe entity. Please note that the proxy entity is tracked by the context that created it. If you didn’tDetachit or usedAsNoTracking()you will not be able to attach it to another context.