1

I'm trying to use this reply https://stackoverflow.com/a/5942176/5741268 For bulk inserts. But this procedure is entering duplicate.

My Entities

public class Empresa
{
    public int EmpresaId { get; set; }
    public string CNPJ { get; set; }
    public string CPF { get; set; }
    public string IE { get; set; }
    public string xNome { get; set; }
    public string cMun { get; set; }
    public string CNAE { get; set; }

    //Endereço
    public string COD_PAIS { get; set; }
    public string UF { get; set; }
    public string CEP { get; set; }
    public string END { get; set; }
    public string NUM { get; set; }
    public string COMPL { get; set; }
    public string BAIRRO { get; set; }
    public string FONE { get; set; }
}

public class NFe
{
    public int NFeId { get; set; }
    //public int NFePartId { get; set; }

    public string versao { get; set; }
    public string chave { get; set; }
    public string ide_mod { get; set; }
    public string ide_serie { get; set; }
    public string ide_nNF { get; set; }
    public DateTime? ide_dEmi { get; set; }
    public DateTime? ide_dSaiEnt { get; set; }
    public string ide_tpNF { get; set; }
    public decimal? TOTAIS_vBC { get; set; }
    public decimal? TOTAIS_vICMS { get; set; }
    public decimal? TOTAIS_vBCST { get; set; }
    public decimal? TOTAIS_vST { get; set; }
    public decimal? TOTAIS_vProd { get; set; }
    public decimal? TOTAIS_vFrete { get; set; }
    public decimal? TOTAIS_vSeg { get; set; }
    public decimal? TOTAIS_vDesc { get; set; }
    public decimal? TOTAIS_vII { get; set; }
    public decimal? TOTAIS_vIPI { get; set; }
    public decimal? TOTAIS_vPIS { get; set; }
    public decimal? TOTAIS_vCOFINS { get; set; }
    public decimal? TOTAIS_vOutro { get; set; }
    public decimal? TOTAIS_vNF { get; set; }
    public string infCpl { get; set; }
    public bool cancelada { get; set; } = false;

    public virtual NFePart NFePart { get; set; }
    public virtual Empresa Empresa { get; set; }
    public virtual ICollection<NFeItem> NFeItemLista { get; set; }
}

public class NFePart
{
    public int NFePartId { get; set; }
    public string NOME { get; set; }
    public string COD_PAIS { get; set; }
    public string CNPJ { get; set; }
    public string IE { get; set; }
    public string UF { get; set; }
    public string CEP { get; set; }
    public string END { get; set; }
    public string NUM { get; set; }
    public string COMPL { get; set; }
    public string BAIRRO { get; set; }
    public string COD_MUN { get; set; }
    public string FONE { get; set; }

    public virtual Empresa Empresa { get; set; }

    public virtual ICollection<NFe> NFe { get; set; }

    //Uso interno, não mapeado no banco de dados
    [NotMapped]
    public int COD_PART { get; set; }
}

Here is my code:

List<NFe> notas = new List<NFe>();

//populate notas
DataContext context = null;

try
{
    List<Empresa> nEmpresas = notas.DistinctBy(x => new { x.Empresa.CNPJ }).Select(x => x.Empresa).ToList();

    foreach (Empresa empresaToAdd in nEmpresas)
    {
        context = new DataContext();
        context.Configuration.AutoDetectChangesEnabled = false;

        int count = 0;
        List<NFe> notasFilter;

        notasFilter = notas.Where(x => x.Empresa.CNPJ == empresaToAdd.CNPJ).ToList();

        int commitCount = 2000;

        foreach (var notaInsert in notasFilter)
        {
            ++count;
            context = AddToContext(context, notaInsert, count, commitCount, true, notasFilter.Count);
        }
    }
}
finally
{
    if (context != null)
        context.Dispose();
}

My AddtoContext function

private static DataContext AddToContext(DataContext context, NFe entity, int count, int commitCount, bool recreateContext, int totalNfe)
{
    //if has already inserted -> Attach entity.Empresa 
    if ((count > commitCount && commitCount != 0) || entity.Empresa.EmpresaId != 0)
    {
        context.Empresa.Attach(entity.Empresa);
    }

    if (context.NFePart.Any(p => p.CNPJ == entity.NFePart.CNPJ && p.Empresa.CNPJ == entity.Empresa.CNPJ))
    {
        context.NFePart.Attach(entity.NFePart);
    }

    context.Set<NFe>().Add(entity);

    if (commitCount == 0 || count % commitCount == 0 || count == totalNfe)
    {
        context.SaveChanges();

        if (recreateContext)
        {
            context.Dispose();
            context = new DataContext();
            context.Configuration.AutoDetectChangesEnabled = false;
        }
    }

    return context;
}

After save changes, the EF insert duplicate

enter image description here

In the code I'm going through all have in "notas" and only filtering the "empresaToAdd" in question. but when the loop moves on to the next "empresaToAdd" it inserts again notes the previous company in the loop even using Dispose, and recreating the context

enter image description here

NFeItemMap

public class NFeItemMap : EntityTypeConfiguration<NFeItem>
{
    public NFeItemMap()
    {
        ToTable("NFeItem");
        HasKey(x => x.NFeItemId);
        Property(x => x.infAdProd).HasColumnType("text").IsMaxLength();


        //one-to-many 
        HasRequired(s => s.NFe) 
            .WithMany(s => s.NFeItemLista); 



        HasRequired(x => x.Empresa)
            .WithMany();             

    }
}
Community
  • 1
  • 1
Bruno Henri
  • 373
  • 1
  • 3
  • 15

0 Answers0