Insert multiple Models in same UnitOfWork transaction.

Jun 8, 2015 at 8:59 PM
Edited Jun 8, 2015 at 8:59 PM
Hi, i want to add more than entity in same transaction:
Marca marca2 = new Marca() { Codigo="100", Descripcion="Insertada a mano", ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added };
Marca marca3 = new Marca() { Codigo = "300", Descripcion = "Insertada a mano 3", ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added };

_assistant.Service.InsertOrUpdateGraph(marca2);
_assistant.Service.InsertOrUpdateGraph(marca3);
_assistant.UnitOfWork.SaveChanges();
And have the following error :

Saving or accepting changes failed because more than one entity of type 'Flota.Entities.Models.Marca' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration

So, we modified the Repository.Pattern.Ef6.Repository class to allow this, is the correct way?
public virtual void InsertOrUpdateGraph(TEntity entity)
{
    SyncObjectGraph(entity);
    _entitesChecked = null;
    if (entity.ObjectState == ObjectState.Added)
          _dbSet.Add(entity);
    else
          _dbSet.Attach(entity);
}