0
Domain
public class Agenda
{
public int Id { get; set; }
public DateTime Data { get; set; }
public string HoraInicial { get; set; }
public string HoraFinal { get; set; }
public string Local { get; set; }
public virtual ICollection<Treino> Treinos { get; set; }
}
public class Treino
{
public int Id { get; set; }
public string Objetivo { get; set; }
public string Descricao { get; set; }
public virtual ICollection<Agenda> Agendas { get; set; }
}
Configurations of Entities
public class TreinoMap : EntityTypeConfiguration<Treino>
{
public TreinoMap()
{
HasMany(x => x.Agendas)
.WithMany(x => x.Treinos)
.Map(x =>
{
x.MapLeftKey("Treino_Id");
x.MapRightKey("Agenda_Id");
x.ToTable("AgendaTreino");
});
}
}
public class AgendaMap : EntityTypeConfiguration<Agenda>
{
public AgendaMap()
{
HasMany(x => x.Treinos)
.WithMany(x => x.Agendas)
.Map(x =>
{
x.MapLeftKey("Agenda_Id");
x.MapRightKey("Treino_Id");
x.ToTable("AgendaTreino");
});
}
}
Method I use to save
public void Inserir(T obj)
{
banco.Set<T>().Add(obj);
banco.SaveChanges();
}
The Agenda object is completed as follows: I normally fill all properties via parameter, with the exception of Property Trainings, it comes null. But in Scheduleviewmodel, I get the training descriptions in a string and convert to Array. Then I pass this array to the method and receive all the Training objects. And, from there, I link these workouts to my Schedule, and, finally, I send save. Below is the treatment of the object.
public ActionResult NovaAgenda(AgendaViewModel agenda)
{
var descricoesDeTreinosEscolhidos = agenda.TreinosEscolhidos.Split(',');
var treinosDomain = _treinoApp.ObterTreinosPorDescricao(descricaoDeTreinosEscolhidos) as IEnumerable<Treino>;
var agendaDomain = MapearAgendaViewModelParaAgenda(agenda);
foreach(var treino in treinosDomain)
{
agendaDomain.Treinos.Add(treino);
}
_agendaApp.Inserir(agendaDomain); // Aqui que lança a exceção.
return View();
}
The following exception is thrown (I could not format with the vertical scroll bar):
An Entity Object cannot be referenced by Multiple instances of Ientitychangetracker. in System.Data.Entity.Core.Objects.ObjectContext.Verifycontextforaddorattach(Ientitywrapper wrappedEntity) em System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName) em System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach) em System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach) em System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach) in System.Data.Entity.Core.Objects.DataClasses.Entitycollection
1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach) em System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach) em System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity) em System.Data.Entity.Internal.Linq.InternalSet
1.<>c__DisplayClassd.b__c() in System.Data.Entity.Internal.Linq.Internalset1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) em System.Data.Entity.Internal.Linq.InternalSet
1.Add(Object Entity) in System.Data.Entity.Dbset1.Add(TEntity entity) em ProjetoTeste.Infra.Data.Repositories.RepositoryBase
1.Insert(T obj) in D: Informática Raphael Projects VS2015 Projectthis Code Projectthis Projectste.Data Repositories Repositorybase.Cs:line 54 in Projetoteste.domain.Services.Servicebase1.Inserir(T obj) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\Codigo\ProjetoTeste\ProjetoTeste.Domain\Services\ServiceBase.cs:linha 24 em ProjetoTeste.Application.Services.AppServiceBase
1.Insert(T obj) in D: Raphael Informatics Projects VS2015 Projectthis Code Projectthis Projectste.Application Services Appservicebase.Cs:line 28 em ProjetoTeste.Presentation.Controllers.AgendaController.NovaAgenda(AgendaViewModel agenda) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\Codigo\ProjetoTeste\ProjetoTeste.Presentation\Controllers\AgendaController.cs:linha 69
It also duplicates http://answall.com/questions/104002/context-tentando-excluir-2-vezes-o-mesmo-registro/104033#104033 and http://answall.com/questions/112941/update-many-to-many-entity-framework-c/112946#112946 and others.
– Leonel Sanches da Silva
By the way, the answer on never use Entity Framework repository has been duly updated to talk about this problem.
– Leonel Sanches da Silva
Thank you very much Gypsy! I understood and it worked here. And I’m sorry, I thought I had to write another question because I wanted an answer regarding the error. And also did not know the name of this error (Highlighted Context) now know hehe.. was even worth
– Raphael