Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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,我的程序正在解析一个xml文件,并将该文件中的订单添加到mssql数据库中。在他将这些订单添加到数据库之前,他会分析这些订单是否存在需要处理的重复项 foreach (var newOrderXml in newOrdersList) { var dupesInDb = _context.OrdersXml.Include(o=>o.OrderXmlItems)

我的程序正在解析一个xml文件,并将该文件中的订单添加到mssql数据库中。在他将这些订单添加到数据库之前,他会分析这些订单是否存在需要处理的重复项

        foreach (var newOrderXml in newOrdersList)
        {                          
            var dupesInDb = _context.OrdersXml.Include(o=>o.OrderXmlItems)
                                              .Where(o => o.OrX_ORDERNR.Contains(newOrderXml.OrX_ORDERNR))
                                              .ToList();
            _context.SaveChanges();
        }
程序循环遍历newOrderList中的所有新订单,并使用linq查询获取重复订单列表。如果有0个重复项且未返回任何内容,则一切正常,但如果返回单个重复项,则SaveChanges方法将引发异常“违反主键约束PK_dbo.SewingCardBundles,无法在对象“dbo.SewingCardBundles”中插入重复项。重复键值为(1)。”,即使我没有在上下文中添加或修改任何内容。我真的不知道发生了什么,我所做的只是获取,我没有改变任何东西,我没有创建新的对象。这个异常正好发生在这个位置,如果我尝试在这个linq查询之前保存更改,那么不会发生什么不好的事情,但是如果我在这个linq查询之后尝试,我会得到异常。那么,这些对上下文的更改是从哪里来的呢

以下是我的模型:

public class OrderXml 
    {
        public OrderXml()
        {
            OrderXmlItems = new List<OrderXmlItem>();
        }

        public int OrX_Id { get; set; }
        public string OrX_ORDERNR { get; set; }
        public string OrX_REFERGB { get; set; }
        public int? OrX_CUSTOMERNUM { get; set; }
        public string OrX_DNAME { get; set; }
        public string OrX_DADR { get; set; }
        public string OrX_DPCODE { get; set; }
        public string OrX_POSTALCODE { get; set; }
        public string OrX_COUNTRY { get; set; }
        public string OrX_PHONE { get; set; }
        public string OrX_EMAIL { get; set; }
        public int? OrX_LANG { get; set; }
        public int? OrX_CUSTGRP { get; set; }
        public int? OrX_QUALITCON { get; set; }
        public string OrX_SHIPVIA { get; set; }
        public string OrX_DATE1 { get; set; }
        public string OrX_DATE2 { get; set; }
        public string OrX_DELIVGB { get; set; }
        public string OrX_SORT { get; set; }
        public int? OrX_CURLAB { get; set; }

        public List<OrderXmlItem> OrderXmlItems { get; set; }

        public Adress Adress { get; set; }
}


public OrderXmlItem()
        {
            SewingCardBundle = new SewingCardBundle();

        }

        public int OxI_Id { get; set; }
        public int? OxI_PRODUCT { get; set; }
        public int? OxI_ORDERLINE { get; set; }
        public int? OxI_QUANTITY { get; set; }
        public int? OxI_TYPE { get; set; }
        public string OxI_TPFABNR { get; set; }
        public string OxI_TPFABDEF { get; set; }
        public string OxI_TPFABNAME { get; set; }
        public int? OxI_CURDIR { get; set; }
        public int? OxI_CURWIDTH { get; set; }
        public int? OxI_CURHEIGHT { get; set; }
        public int? OxI_WORKMETH { get; set; }
        public int? OxI_FOLDTYPE { get; set; }
        public decimal? OxI_FOLDFACT { get; set; }
        public int? OxI_CURBAND { get; set; }
        public int? OxI_CURHEAD { get; set; }
        public int? OxI_CURBOTSEAM { get; set; }
        public int? OxI_PACKWLEFT { get; set; }
        public int? OxI_PACKWRIGHT { get; set; }
        public decimal? OxI_NRSTROL { get; set; }
        public decimal? OxI_NRSTROR { get; set; }
        public int? OxI_LINTYP { get; set; }
        public string OxI_LINCOL { get; set; }
        public int? OxI_EMBSORT { get; set; }
        public int? OxI_EXTRA { get; set; }
        public int? OxI_PRODUCE { get; set; }
        public int? OxI_PACKSORT { get; set; }
        public int? OxI_CURMODEL { get; set; }
        public string OxI_BARCODE { get; set; }
        public string OxI_EXTRAINF { get; set; }
        public int? OxI_RAILTYP { get; set; }
        public int? OxI_RAILCONT { get; set; }
        public int? OxI_RAILCONTSIDE { get; set; }
        public decimal? OxI_FABSTROTOT { get; set; }
        public decimal? OxI_FABSTROLEFT { get; set; }
        public decimal? OxI_FABSTRORIGHT { get; set; }
        public int? OxI_FABUNDSIZ { get; set; }
        public int? OxI_FABTOTSIZ { get; set; }
        public int? OxI_LINSTROTOT { get; set; }
        public int? OxI_LINUNDSIZ { get; set; }
        public int? OxI_LINTOTSIZ { get; set; }
        public decimal? OxI_FABWIDTH { get; set; }
        public int? OxI_CHILDSFT { get; set; }
        public int? OxI_FOLDSORT { get; set; }
        public int? OxI_EMBLENGTH { get; set; }
        public int? OxI_PACKMETH { get; set; }


        public int OrderXmlId { get; set; }
        public OrderXml OrderXml { get; set; }


        public SewingCardBundle SewingCardBundle { get; set; }
    }


public class SewingCardBundle
    {
        public SewingCardBundle()
        {
            FlamanSewingCards = new List<FlamandzkaSewingCard>();
            FlamandzkaBrytaSewingCards = new List<FlamandzkaBrytaSewingCard>();
            OczkaSewingCards = new List<OczkaSewingCard>();
            OczkaBrytaSewingCards = new List<OczkaBrytaSewingCard>();
            WellenbandSewingCards = new List<WellenbandSewingCard>();
            WellenbandBrytaSewingCards = new List<WellenbandBrytaSewingCard>();
            PodwiazkaSewingCards = new List<PodwiazkaSewingCard>();
            TunelSewingCards = new List<TunelSewingCard>();
        }

        public int SwC_Id { get; set; }

        public OrderXmlItem OrderXmlItem { get; set; }

        public List<FlamandzkaSewingCard> FlamanSewingCards { get; set; }
        public List<FlamandzkaBrytaSewingCard> FlamandzkaBrytaSewingCards { get; set; }
        public List<OczkaSewingCard> OczkaSewingCards { get; set; }
        public List<OczkaBrytaSewingCard> OczkaBrytaSewingCards { get; set; }
        public List<WellenbandSewingCard> WellenbandSewingCards { get; set; }
        public List<WellenbandBrytaSewingCard> WellenbandBrytaSewingCards { get; set; }
        public List<PodwiazkaSewingCard> PodwiazkaSewingCards { get; set; }
        public List<TunelSewingCard> TunelSewingCards { get; set; }
    }
公共类OrderXml
{
公共命令XML()
{
OrderXmlItems=新列表();
}
公共int OrX_Id{get;set;}
公共字符串OrX_ORDERNR{get;set;}
公共字符串OrX_referegb{get;set;}
公共int?OrX_CUSTOMERNUM{get;set;}
公共字符串OrX_DNAME{get;set;}
公共字符串OrX_DADR{get;set;}
公共字符串OrX_DPCODE{get;set;}
公共字符串OrX_POSTALCODE{get;set;}
公共字符串OrX_COUNTRY{get;set;}
公共字符串OrX_PHONE{get;set;}
公共字符串OrX_电子邮件{get;set;}
公共int?OrX_LANG{get;set;}
公共int?OrX_CUSTGRP{get;set;}
公共int?OrX_QUALITCON{get;set;}
公共字符串OrX_SHIPVIA{get;set;}
公共字符串OrX_DATE1{get;set;}
公共字符串OrX_DATE2{get;set;}
公共字符串OrX_{get;set;}
公共字符串OrX_排序{get;set;}
公共int?OrX_CURLAB{get;set;}
公共列表OrderXmlItems{get;set;}
公共地址地址{get;set;}
}
公共医嘱XmlItem()
{
SewingCardBundle=新的SewingCardBundle();
}
公共int-OxI_-Id{get;set;}
公共整数?OxI_乘积{get;set;}
公共整数?OxI_命令行{get;set;}
公共整数?OxI_数量{get;set;}
公共整数?OxI_类型{get;set;}
公共字符串OxI_TPFABNR{get;set;}
公共字符串oxiu TPFABDEF{get;set;}
公共字符串OxI_TPFABNAME{get;set;}
公共整数?OxI_CURDIR{get;set;}
公共整数?OxI_CURWIDTH{get;set;}
公共整数?OxI_CURHEIGHT{get;set;}
公共整数?OxI_工作方法{get;set;}
公共整数?OxI_FOLDTYPE{get;set;}
公共小数?OxI_FOLDFACT{get;set;}
公共整数?OxI_和{get;set;}
公共整数?OxI_CURHEAD{get;set;}
公共整数?OxI_路缘接缝{get;set;}
公共整数?OxI_PACKWLEFT{get;set;}
公共整数?OxI_PACKWRIGHT{get;set;}
公共小数?OxI_NRSTROL{get;set;}
公共小数?OxI_nrstor{get;set;}
公共整数?OxI_LINTYP{get;set;}
公共字符串OxI_LINCOL{get;set;}
公共整数?OxI_EMBSORT{get;set;}
公共整数?OxI_EXTRA{get;set;}
公共整数?OxI_生成{get;set;}
公共整数?OxI_PACKSORT{get;set;}
公共整数?OxI_CURMODEL{get;set;}
公共字符串OxI_条形码{get;set;}
公共字符串OxI_EXTRAINF{get;set;}
公共整数?OxI_RAILTYP{get;set;}
公共整数?OxI_RAILCONT{get;set;}
公共整数?OxI_RAILCONTSIDE{get;set;}
公共小数?OxI_FABSTROTOT{get;set;}
公共小数?OxI_FABSTROLEFT{get;set;}
公共小数?OxI_FABSTRORIGHT{get;set;}
公共整数?OxI_FABUNDSIZ{get;set;}
公共整数?OxI_FABTOTSIZ{get;set;}
公共整数?OxI_linstrott{get;set;}
公共整数?OxI_LINUNDSIZ{get;set;}
公共整数?OxI_LINTOTSIZ{get;set;}
公共小数?OxI_{get;set;}
公共整数?OxI_CHILDSFT{get;set;}
公共整数?OxI_FOLDSORT{get;set;}
公共整数?OxI_EMBLENGTH{get;set;}
公共整数?OxI_PACKMETH{get;set;}
公共int-OrderXmlId{get;set;}
public OrderXml OrderXml{get;set;}
公共缝合cardbundle缝合cardbundle{get;set;}
}
公共类缝制卡片包
{
公共缝合卡束()
{
FlamanSewingCards=新列表();
FlamandzCabryTaseWingCards=新列表();
OczkaSewingCards=新列表();
OczkaBrytaSewingCards=新列表();
WellenbandSewingCards=新列表();
WellenbandBrytaSewingCards=新列表();
PodwiazkaSewingCards=新列表();
tunelswingcards=新列表();
}
public int SwC_Id{get;set;}
公共OrderXmlItem OrderXmlItem{get;set;}
公共列表{get;set;}
公共列表FlamandzCabryTaseWingCards{get;set;}
公共列表OczkaSewingCards{get;set;}
公共列表OczkaBrytaSewingCards{get;set;}
公共列表WellenbandSewingCards{get;set;}
公共列表WellenbandBrytaSewingCards{get;set;}
公共列表PodwiazkaSewingCards{get;set;}
公共列表{get;set;}
}
以及我针对这些模型的Fluent API配置:

     public class OrderXmlConfiguration : EntityTypeConfiguration<OrderXml>
            {
                public OrderXmlConfiguration()
                {
                    HasKey(o => o.OrX_Id);

                    Property(o => o.OrX_ORDERNR).IsRequired();
                    Property(o => o.OrX_REFERGB).IsRequired();
                    Property(o => o.OrX_CUSTOMERNUM).IsRequired();
                    Property(o => o.OrX_DNAME).IsRequired();
                    Property(o => o.OrX_DPCODE).IsRequired();
                    Property(o => o.OrX_POSTALCODE).IsRequired();
                    Property(o => o.OrX_COUNTRY).IsRequired();
                    Property(o => o.OrX_LANG).IsRequired();
                    Property(o => o.OrX_CUSTGRP).IsRequired();
                    Property(o => o.OrX_SHIPVIA).IsRequired();
                    Property(o => o.OrX_CURLAB).IsRequired();

                    HasMany(i => i.OrderXmlItems)
                        .WithRequired(o => o.OrderXml)
                        .HasForeignKey(o => o.OrderXmlId)
                        .WillCascadeOnDelete(true);           
                }

            }

    public class OrderXmlItemConfiguration : EntityTypeConfiguration<OrderXmlItem>
    {
        public OrderXmlItemConfiguration()
        {
            HasKey(o => o.OxI_Id);

            Property(p => p.OxI_Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(p => p.OxI_PRODUCT).IsRequired();
            Property(p => p.OxI_ORDERLINE).IsRequired();
            Property(p => p.OxI_QUANTITY).IsRequired();
            Property(p => p.OxI_TYPE).IsRequired();
            Property(p => p.OxI_CURDIR).IsRequired();
            Property(p => p.OxI_CURWIDTH).IsRequired();
            Property(p => p.OxI_CURHEIGHT).IsRequired();
            Property(p => p.OxI_WORKMETH).IsRequired();
            Property(p => p.OxI_FOLDTYPE).IsRequired();
            Property(p => p.OxI_FOLDFACT).IsRequired();
            Property(p => p.OxI_PACKWLEFT).IsRequired();
            Property(p => p.OxI_PACKWRIGHT).IsRequired();
            Property(p => p.OxI_BARCODE).IsRequired();


            HasRequired(i => i.SewingCardBundle)
                .WithRequiredPrincipal( s=> s.OrderXmlItem)
                .WillCascadeOnDelete(true);


        }

    }

public class SewingCardBundleConfiguration : EntityTypeConfiguration<SewingCardBundle>
    {
        public SewingCardBundleConfiguration()
        {

            HasKey(s => s.SwC_Id);

            HasMany(s=>s.FlamanSewingCards)
                .WithRequired(c=>c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.FlamandzkaBrytaSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.OczkaBrytaSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.OczkaSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.WellenbandSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.WellenbandBrytaSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.TunelSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);

            HasMany(s => s.PodwiazkaSewingCards)
                .WithRequired(c => c.SewingCardBundle)
                .WillCascadeOnDelete(true);


        }
    }
公共类OrderXmlConfiguration:EntityTypeConfiguration { public OrderXmlConfiguration()
var dupesInDb = _context
   .OrdersXml.Include(o=>o.OrderXmlItems)
   .Where(o => o.OrX_ORDERNR.Contains(newOrderXml.OrX_ORDERNR))
   .AsNoTracking()
   .ToList();