ICollectionView throw Entity Framework Attach exception

Aug 22, 2014 at 9:57 PM
Edited Aug 22, 2014 at 9:57 PM
Link: Stack Overflow Link

I dont know if its a bug of the UoW of this repository to not dispose it very well...

Thanks!
Coordinator
Aug 22, 2014 at 10:57 PM
This was fixed in the v3.3.2 (latest), issues w/ change sets.


With full integration tests


Please let us know if this is still an issue and steps to reproduce this.

Aug 25, 2014 at 3:03 PM
Edited Aug 25, 2014 at 3:05 PM
Updated to v3.3.2 but same problem

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Attaching an entity of type 'Sistema.DataEntities.Models.Cliente' failed because another entity 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 entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

Image
Sep 1, 2014 at 7:30 PM
Edited Sep 1, 2014 at 8:46 PM
Please help me... i really need a help on this error.

when the exception occours, the _var existing = dbSet.Local; is full of elements(this code i put 1 line before of the _dbSet.Attach... for debug)
and the attach throw a exception because of it.

The element that im attaching is inside of the var existing.
Coordinator
Sep 2, 2014 at 1:01 AM
Hi Daniloloko,

From your post I can't derive what's going on, if this is urgent, you can request a one on one live support session for this issue from the "Documents" page.
Sep 2, 2014 at 2:05 PM
Hi lelong,

Thats not a urgent thing, im just WTF is going on...

to reproduce this thats the code:
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.InjectFrom<SmartConventionInjection>(obCliente);

cliConvertido.Nome = "A" + rand.Next(999999, 9999999) + " BREDA";

Cliente obCliente = new Cliente();

obCliente.InjectFrom<SmartConventionInjection>(cliConvertido);

_clienteService.ClienteService_Update(obCliente);
Thanks
Sep 2, 2014 at 4:06 PM
Edited Sep 3, 2014 at 6:54 PM
The problem was that this framework doesn't fit for windows application...

"since the framework wraps the DbSet<T>, you can't use it, I think this framework doesn't fit for windows application, unless you change the way you instantiate the IClienteService into per operation (per method call) "

I will need to use the AsNoTracking()...
The Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking(). This optimisation allows you to tell Entity Framework not to track the results of a query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However it also means that you cant update these entities without reattaching them to the tracking graph.
Sep 3, 2014 at 6:49 PM
Edited Sep 3, 2014 at 6:55 PM
Problem resolved...
public IEnumerable<Cliente> ClienteService_GetAll()
        {
            return _repository.Query().Select().AsEnumerable();
        }
The Repository.cs has a internal IQueryable<TEntity> Select(.... I added the AsNoTracking() on this line:
_IQueryable<TEntity> query = dbSet.AsNoTracking();

Now when i update my object using:
public virtual void Update(TEntity entity)
        {
            var existing = _dbSet.Local;// NOW LOCAL IS NULL 

            entity.ObjectState = ObjectState.Modified;
            _dbSet.Attach(entity);//no exceptions here
            _context.SyncObjectState(entity);
        }
Im waiting a back message. Thanks
Marked as answer by Daniloloko on 9/3/2014 at 10:54 AM
Nov 3, 2014 at 9:14 PM
..I tried the dbSet.AsNoTracking(). But Still continue to get the ""System.InvalidOperationException: Attaching an entity of type 'NS.Models.EntityName' failed because another entity 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 entity to 'Unchanged' or 'Modified' if any e........."

I can see that my "_dbSet.Local" has items in it. Even though I am setting the ObjectState to ObjectState.Modified before the Attach, the update still failed with the above error.

Any feedback will be appreciated.

thanks.
Coordinator
Nov 3, 2014 at 10:12 PM
Could you post your AsNoTracking implementation?
Nov 4, 2014 at 2:34 AM
same thing as 'Daniloloko' did above.

"The Repository.cs has a internal IQueryable<TEntity> Select(.... I added the AsNoTracking() on this line: _IQueryable<TEntity> query = dbSet.AsNoTracking(); "

...mind u I am using your architecture (nice) successfully in 2-3 other projects, but then i upgraded to EF 6.1.1 due to another issues.
but i don't see how that matters for this issue.

I am using .NET 4.5.1
  • could it be that I am using EF 6.1.1 ?
  • should I be using a different EF version ?
  • should I upgrade to your latest version i.e. 3.3.2 ?
...I might sleep better tonight if I get a solution :-)
Nov 4, 2014 at 4:10 AM
Edited Nov 4, 2014 at 4:23 AM
I never get this running... but i found the problem.... the Entity framework... I have a full error explanation: Stackoverflow

But i never found the solution... and i maked my project again zero using EF directly... i teste it a little and jumped to UI to not kill my time... i think to find a solution on the future...

UPDATE
This framework does not fit very well on WPF MVVM applications... because of ICollectionView (PRISM 5).
What you can do is remove your mapping or valueinjector from your archteture(if you are using MVVM+WPF) and use the MVVM Model as the same as the EF entities. Resolved.(or half of it)