When I try to save an object in EF, it throws this exception:
An exception of type "System.InvalidOperationException" occurred in EntityFramework.dll, but was not processed in the user code.
Additional information: the binding of an object of type 'Sistema.DataEntities.Models.Cliente' failed because another object of the same type already has the same primary key value. This can happen when using the “Attach” method or setting the state of an object to “No change” or “Modified” if any objects on the chart have conflicting key values. This may be due to the fact that some objects are new and have not yet received the database key values. In this case, use the "Add" or "Added" state of the object to track the chart and then set the state of not new objects to "No change" or "Modified" as necessary.

'cliItems = ListCollectionView (t.ToList()); , ListCollectionView PRISM.
public class CadastroClienteViewModel : BindableBase, ICadastroClienteViewModel
{
private readonly IClienteService _clienteService;
public CadastroClienteViewModel(IClienteService ServiceCliente)
{
_clienteService = ServiceCliente;
this.SaveCommand = new DelegateCommand(ExecuteMethodSave);
this.RefreshCommand = new DelegateCommand(ExecuteMethodRefresh, CanExecuteMethodRefresh);
RefreshCommand.Execute(null);
}
private void ExecuteMethodSave()
{
Sistema.DataEntities.Models.Cliente clifinal = new Sistema.DataEntities.Models.Cliente();
clifinal.InjectFrom<UnflatLoopValueInjection>(ObCliente);
_clienteService.ClienteService_Update(clifinal);
RefreshCommand.Execute(null);
}
private bool CanExecuteMethodRefresh()
{
return true;
}
private void ExecuteMethodRefresh()
{
var t = _clienteService.ClienteService_GetAll().Select(p => new Cliente().InjectFrom<FlatLoopValueInjection>(p));
cliItems = new ListCollectionView(t.ToList());
cliItems.CurrentChanged += cliItemsOnCurrentChanged;
OnPropertyChanged("cliItems");
}
private void cliItemsOnCurrentChanged(object sender, EventArgs eventArgs)
{
ObCliente = (Cliente)cliItems.CurrentItem;
this.OnPropertyChanged("ObCliente");
}
public ICommand SaveCommand { get; private set; }
public ICommand RefreshCommand { get; private set; }
public Cliente ObCliente { get; private set; }
public ICollectionView cliItems { get; private set; }
}
(-) :
public class ClienteService : Common.Services.Service<Cliente>, IClienteService
{
private readonly IRepositoryAsync<Cliente> _repository;
private readonly IUnitOfWorkAsync _uow;
public ClienteService(IRepositoryAsync<Cliente> repository, IUnitOfWorkAsync uow)
: base(repository)
{
_repository = repository;
_uow = uow;
}
public void ClienteService_Adicionar(Cliente Obcliente)
{
_repository.Insert(Obcliente);
_uow.SaveChanges();
}
public void ClienteService_Update(Cliente Obcliente)
{
Obcliente.ObjectState = ObjectState.Modified;
_repository.Update(Obcliente);
_uow.SaveChanges();
}
public IEnumerable<Cliente> ClienteService_GetAll()
{
var t = _repository.Query().Select().AsEnumerable();
return t;
}
}
Repository.cs o :
public virtual void Update(TEntity entity)
{
((IObjectState)entity).ObjectState = ObjectState.Modified;
_dbSet.Attach(entity);
_context.SyncObjectState(entity);
}
Im, () Framework
.
ViewModels Entity im Value Injecter
( + UNITY + Prism)

UPDATE:
:
IEnumerable<Cliente> Clientes = _clienteService.ClienteService_GetAll();
var personViewModels = new List<Sistema.MVVMModels.CadastroModule.Cliente>().InjectFrom(Clientes);
Sistema.MVVMModels.CadastroModule.Cliente cliConvertido = personViewModels.SingleOrDefault(x => x.ClienteID == 1);
cliConvertido.Nome = "A" + rand.Next(999999, 9999999) + " BREDA";
Cliente obCliente = new Cliente();
obCliente.InjectFrom<SmartConventionInjection>(cliConvertido);
_clienteService.ClienteService_Update(obCliente);
:
answere .
Repository.cs IQueryable Select (.... AsNoTracking() :
IQueryable<TEntity> query = _dbSet.AsNoTracking();
, , :
public virtual void Update(TEntity entity)
{
var existing = _dbSet.Local;
entity.ObjectState = ObjectState.Modified;
_dbSet.Attach(entity);
_context.SyncObjectState(entity);
}