Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架正在进入批量插入重复方法_C#_Entity Framework - Fatal编程技术网

C# 实体框架正在进入批量插入重复方法

C# 实体框架正在进入批量插入重复方法,c#,entity-framework,C#,Entity Framework,我正在尝试将此回复用于批量插入。但这个过程正在重复 我的实体 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

我正在尝试将此回复用于批量插入。但这个过程正在重复

我的实体

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; }
}
public class Empresa
{
public int EmpresaId{get;set;}
公共字符串CNPJ{get;set;}
公共字符串CPF{get;set;}
公共字符串IE{get;set;}
公共字符串xNome{get;set;}
公共字符串cMun{get;set;}
公共字符串CNAE{get;set;}
//恩德雷索
公共字符串COD_PAIS{get;set;}
公共字符串UF{get;set;}
公共字符串CEP{get;set;}
公共字符串结束{get;set;}
公共字符串NUM{get;set;}
公共字符串COMPL{get;set;}
公共字符串BAIRRO{get;set;}
公共字符串FONE{get;set;}
}
公共类NFe
{
公共int NFeId{get;set;}
//public int NFePartId{get;set;}
公共字符串versao{get;set;}
公共字符串chave{get;set;}
公共字符串ide_mod{get;set;}
公共字符串ide_serie{get;set;}
公共字符串ide_nNF{get;set;}
公共日期时间?ide_dEmi{get;set;}
公共日期时间?ide_dSaiEnt{get;set;}
公共字符串ide_tpNF{get;set;}
公共十进制?TOTAIS_vBC{get;set;}
公共十进制?TOTAIS_vICMS{get;set;}
公共十进制?TOTAIS_vBCST{get;set;}
公共十进制?TOTAIS_vST{get;set;}
公共小数?TOTAIS_vProd{get;set;}
公共十进制?TOTAIS_vFrete{get;set;}
公共十进制?TOTAIS_vSeg{get;set;}
公共十进制?TOTAIS_vDesc{get;set;}
公共小数?TOTAIS_vII{get;set;}
公共十进制?TOTAIS_vIPI{get;set;}
公共十进制?TOTAIS_vPIS{get;set;}
公共十进制?TOTAIS_vCOFINS{get;set;}
公共十进制?TOTAIS_vOutro{get;set;}
公共十进制?TOTAIS_vNF{get;set;}
公共字符串infCpl{get;set;}
公共bool cancelada{get;set;}=false;
公共虚拟NFePart NFePart{get;set;}
公共虚拟Empresa Empresa{get;set;}
公共虚拟ICollection NFeItemLista{get;set;}
}
公共类非配件
{
public int NFePartId{get;set;}
公共字符串NOME{get;set;}
公共字符串COD_PAIS{get;set;}
公共字符串CNPJ{get;set;}
公共字符串IE{get;set;}
公共字符串UF{get;set;}
公共字符串CEP{get;set;}
公共字符串结束{get;set;}
公共字符串NUM{get;set;}
公共字符串COMPL{get;set;}
公共字符串BAIRRO{get;set;}
公共字符串COD_MUN{get;set;}
公共字符串FONE{get;set;}
公共虚拟Empresa Empresa{get;set;}
公共虚拟ICollection NFe{get;set;}
//国际银行,não mapeado no banco de dados
[未映射]
公共int codu部分{get;set;}
}
这是我的密码:

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();
}
List notas=新列表();
//填充notas
DataContext=null;
尝试
{
列出nEmpresas=notas.DistinctBy(x=>new{x.Empresa.CNPJ})。选择(x=>x.Empresa.ToList();
foreach(Empresa Empresatoad in nEmpresas)
{
context=newdatacontext();
context.Configuration.AutoDetectChangesEnabled=false;
整数计数=0;
列表注释过滤器;
notasFilter=notas.Where(x=>x.Empresa.CNPJ==empresatoad.CNPJ).ToList();
int commitCount=2000;
foreach(变量NOTAS插入notasFilter)
{
++计数;
context=AddToContext(context,notairst,count,commitCount,true,notasFilter.count);
}
}
}
最后
{
if(上下文!=null)
context.Dispose();
}
我的AddtoContext函数

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;
}
private static DataContext AddToContext(DataContext上下文、NFe实体、int count、int commitCount、bool recreateContext、int totalNfe)
{
//如果已插入->附加实体.Empresa
如果((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().Add(实体);
如果(commitCount==0 | |计数%commitCount==0 | |计数==totalNfe)
{
SaveChanges();
如果(重新创建上下文)
{
context.Dispose();
context=newdatacontext();
context.Configuration.AutoDetectChangesEnabled=false;
}
}
返回上下文;
}
保存更改后,EF insert将重复

在代码中,我将浏览所有“notas”中的内容,并且只过滤有问题的“empresaToAdd”。但当循环转到下一个“empresaToAdd”时,它会再次插入循环中的上一个公司,甚至使用Dispose并重新创建上下文

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();             

    }
}
公共类NFeItemMap:EntityTypeConfiguration
{
公共NFeItemMap()
{
ToTable(“NFeItem”);
HasKey(x=>x.NFeItemId);
属性(x=>x.infAdProd).HasColumnType(“文本”).IsMaxLength();
//一对多
HasRequired(s=>s.NFe)
.有许多(s=>s.NFeItemLista);
HasRequired(x=>x.Empresa)
.有许多();
}
}

我认为您没有使用最佳解决方案进行大容量插入,我的第一印象是您的数据库映射不正确。我使用地图代码编辑了我的地图。我认为您没有使用最佳解决方案进行大容量插入,我的第一印象是您的数据库映射不正确。我使用地图代码进行了编辑