I think the problem is when trying to assign an instance of the object and an id at the same time, try commenting on the following line of code: { Etapa etapaFilha = null; using (var context = new ContextEtapa()) { etapaFilha = await context.Etapa.Where(x => x.Id == 666).First(); } Etapa etapa = new Etapa(); etapa.Descricao = "test"; etapa.Ativo = true; etapa.Finalizadora = true; etapa.ListaEtapaVinculada = new List<EtapaVinculada>(); EtapaVinculada etapaVinculada = new EtapaVinculada(); // etapaVinculada.EtapaPaiId = etapa.Id; // this is asigned when asign to collection and savechanges // etapaVinculada.EtapaPai = etapa; etapaVinculada.EtapaFilhaId = etapaFilha.Id; // // etapaVinculada.EtapaFilha = etapaFilha; this is duplicate etapa.listaEtapaVinculada.Add(etapaVinculada); using (var context = new ContextoEtapa()) { await context.Etapa.AddAsync(etapa); await context.SaveChangesAsync(); }} 这篇关于实体框架-关键字"PRIMARY"的重复条目"1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 13:24