2
At some point in the system some records are selected and should be replicated by changing only one value.
At this point I am selecting the records I need to enter:
listaRegistros = listaRegistros .GroupBy(r => r.Id)
.Select(grp => grp.First()).ToList();
Soon after I use these selected records above to insert new records in the table:
foreach (var orgao in listaOrgaosASeremAdicionados)
{
foreach (var registro in listaRegistros)
registro.Orgao = orgao;
new RegistrosMeService().InsertListaRegistrosMe(listaRegistros);
}
Here is the InsertListaRegistrosMe
class RegistrosMeService
:
protected EFSpecificRepository<TB_RegistrosMe, Entities> _repository;
protected EFSpecificRepository<TB_RegistrosMe, Entities> Repository
{
get
{
if (_repository == null)
_repository = new EFSpecificRepository<TB_RegistrosMe, Entities>();
return _repository;
}
}
public int InsertListaRegistrosMe(List<TB_RegistrosMe> listaMe)
{
Repository.InsertList(listaMe);
Repository.SaveChanges();
}
The first loop
, where the insertion is made, it is to inform to how many organs the list of records will be added, more than one may have been selected.
The only change made in the record is the one that can be verified in the loop
, which is the amendment of registro.Orgao
.
The problem is that in Insert the error occurs:
"There is already an object with the same key in Objectstatemanager. The object is in the Unchanged state. An object can only be added again to Objectstatemanager if it is in the added state."
I have tried to change Id, but while trying to do this also an error occurs:
'Id' property is part of the object’s key information and not can be modified.
From what I understand this problem occurs because I am observing a record that already exists in EF and for security it does not allow me to add this record because it considers it duplicated, correct?
But how could I solve this without creating a new object in EF 3.5 (v1)? It is possible to change the ObjectStateManager
so that he can do the insertion?
@jbueno, I checked for my
System.Data.Entity
and that’s the same version. Another strange fact if it really doesn’t exist istag
be here at Sopt, but really when I search for this version I find nothing concrete about it. But I found here– George Wurthmann
What’s in
RegistrosMeService.InsertListaRegistrosMe
?– Leonel Sanches da Silva
@Romaniomorrisonmendez is where are all methods of select, Insert, saveChanges. Want I edit with the method
InsertListaRegistrosMe
?– George Wurthmann
Yes, please. That’s the problem.
– Leonel Sanches da Silva
@Ciganomorrisonmendez I added the pegunta.
– George Wurthmann
Another thing: there is no EF3.5. The first stable EF is 4.1, which should be what you are using.
– Leonel Sanches da Silva
@Ciganomorrisonmendez then version check by
System.Data.Entity
is not correct? How do I identify the version correctly?– George Wurthmann
Library
EntityFramework
, in your References, or version of the filepackages.config
.– Leonel Sanches da Silva