Entity Framework 6 Many to Many: PK validation problem when adding

0

Good morning guys, I'm breaking my head here and I still can not fix it.

I have two entities, PropertyDetails and PropertyDetails, with relationship n to n. I have been populated, so when I am going to register the ImovelDetalhe, I select through checkbox the ImovelDetalheBase that the ImovelDetalhe will have, when carrying out the process of recording it it returns the error of PK Validation Error, warning that the PK already exists in the ImovelDetalheBase and closes the process, that is, it is trying to write the record in the FlatBase also being that it does not need, I just want it to record the FlatIndividualIndoveIndoveBase (relationship support table), the FlatIndigoBaseIt does not need to be already populated. How do I resolve this?

Entities:

public class ImovelDetalhe
{
    public ImovelDetalhe()
    {
        Id = Guid.NewGuid();
        DetalheBase = new List<ImovelDetalheBase>();
    }
    public Guid Id { get; set; }
    public string Descricao { get; set; }

    public List<ImovelDetalheBase> DetalheBase { get; set; }

}

public class ImovelDetalheBase
{
    public ImovelDetalheBase()
    {
        Id = Guid.NewGuid();
    }
    public Guid Id { get; set; }
    public string Descricao { get; set; }
    public EnumTipoDados TipoDados { get; set; }
    public string Valor { get; set; }
}

Relationship Settings = FluentApi:

public class ImovelDetalheConfig: EntityTypeConfiguration<ImovelDetalhe>
{
    public ImovelDetalheConfig()
    {
        HasKey(p => p.Id);
        Property(p => p.Descricao)
            .IsRequired();
        HasMany(p => p.DetalheBase)
            .WithMany()
            .Map(d =>
            {
                d.MapLeftKey("DetalheBaseId");
                d.MapRightKey("DetalheId");
                d.ToTable("ImovelDetalhe_ImovelDetalheBase");
            });
        ToTable("ImovelDetalhes");

    }
}

public class ImovelDetalheBaseConfig: EntityTypeConfiguration<ImovelDetalheBase>
{
    public ImovelDetalheBaseConfig()
    {
        HasKey(p => p.Id);
        Property(p => p.Descricao)
            .IsRequired()
            .HasMaxLength(150);
        Property(p => p.TipoDados)
            .IsRequired();
        Property(p => p.Valor)                
            .HasMaxLength(150);
        ToTable("ImovelDetalheBases");
    }

}

Controller part:

public ActionResult Create()
    {
        var imovelDetalhe = new ImovelDetalheViewModel();
        PreencherImovelDetalheBase(imovelDetalhe);
        return View(imovelDetalhe);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ImovelDetalheViewModel imovelDetalhe, string[] imovelDetalheBaseSelecionado)
    {
        if (ModelState.IsValid)
        {
            _service.Adicionar(imovelDetalhe, imovelDetalheBaseSelecionado);
            return RedirectToAction("Index");
        }

        return View(imovelDetalhe);
    }

Part of the service called by the controller:

public void Adicionar(ImovelDetalheViewModel obj, string[] imovelDetalheBaseLista)
    {
        var imovelDetalhe = Mapper.Map<ImovelDetalhe>(obj);
        if (imovelDetalheBaseLista != null)
        {
            foreach (var item in imovelDetalheBaseLista)
            {
                if (item != "false")
                {
                    var busca = repositoryImovelDetalheBase.ObterPorId(Guid.Parse(item));
                    repositoryImovelDetalheBase.EntityStateToUnmodified(busca);
                    imovelDetalhe.DetalheBase.Add(busca);
                }                    
            }
        }

        repositoryImovelDetalhe.Adicionar(imovelDetalhe);
    }

Part of the Repository using the Generic Repository:

public void Adicionar(TEntity tentity)
    {
        dbSet.Add(tentity);
        context.SaveChanges(); //Erro de PK Validation Error, duplicated PK
    }
    
asked by anonymous 13.07.2018 / 17:55

0 answers