加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – SaveChanges()正在保存未更改的记录

发布时间:2020-12-15 04:26:36 所属栏目:百科 来源:网络整理
导读:我不知道我是否遗漏了某些东西……无论如何: 例如,您可以看到具有属性’HomeTeam’=’Forest Green Rovers’的对象具有state =’Unchanged’.无论如何,在我的情况下,所有实体都“保持不变”.所以,如果我是正确的,saveChanges不应该尝试在我的表中插入那些,
我不知道我是否遗漏了某些东西……无论如何:

例如,您可以看到具有属性’HomeTeam’=’Forest Green Rovers’的对象具有state =’Unchanged’.无论如何,在我的情况下,所有实体都“保持不变”.所以,如果我是正确的,saveChanges不应该尝试在我的表中插入那些,但这就是它发生的事情:

主键已被违反,但EF不应该尝试添加此记录(属性为HomeTeam =’Forest Green Rovers’),因为它是’Unchanged’,对吗?

为什么EF这样做?

实体:

{
using System;
using System.Collections.Generic;

public partial class MatchInfo
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2214:DoNotCallOverridableMethodsInConstructors")]
    public MatchInfo()
    {
        this.Sport = "Soccer";
        this.OddsInfoes1X2 = new HashSet<OddsInfo1X2>();
        this.Odds1X2Movements = new HashSet<OddsMovement>();
        this.OddsInfoOverUnders = new HashSet<OddsInfoOverUnder>();
        this.OddsMovementOverUnders = new HashSet<OddsMovementOverUnder>();
        this.HistoryResults = new HashSet<HistoryResult>();
    }

    public string League { get; set; }
    public System.DateTime Date { get; set; }
    public string HomeTeam { get; set; }
    public string AwayTeam { get; set; }
    public string FinalScore { get; set; }
    public string Sport { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OddsInfo1X2> OddsInfoes1X2 { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OddsMovement> Odds1X2Movements { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OddsInfoOverUnder> OddsInfoOverUnders { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OddsMovementOverUnder> OddsMovementOverUnders { get; set; }
    public virtual TeamsStatFinal TeamsStatFinal { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<HistoryResult> HistoryResults { get; set; }
}

}

更多信息:

foreach (something){
     /*iterating on a web page and where I build the object oMatchInfo */
     if (oMatchInfo != null)
     {
          oMatchInfoTemp = (from m in context.MatchInfoes where m.HomeTeam == oMatchInfo.HomeTeam && m.AwayTeam == oMatchInfo.AwayTeam && m.Date == oMatchInfo.Date select m).FirstOrDefault<MatchInfo>();
          if (oMatchInfoTemp == null)
          {
              context.MatchInfoes.Add(oMatchInfo);
          }
          else
          {                                         
              context.OddsInfoes1X2.AddRange(oMatchInfo.OddsInfoes1X2);
          }
     }

}
/* here there is the context.saveChanges() - the context is the same */

欢声笑语.我发现了我的错误,它真的很愚蠢:-(在一个相关的函数里面有一些未注释的代码,我在那里添加实体而不检查PK约束.
国家’Unchanged'(第一张图片)让我困惑,我一直专注于那……
对不起:-)

解决方法

仍然无法从你的代码中分辨出来,因为你的表现不够.

您需要为我们提供更多代码,以便了解您为什么要调用它.

我希望这段代码可以帮助你理解,这对你来说不是一个解决方案,我只是想表明你的for-each看起来不对,取决于你在做什么,但是因为你没有展示所有的代码,我们不能告诉.

public void Save(TEntity entity) //change this to your entity
    {
        if(entity == null)
            return;

        //change this to your key,this is the identifier for new
        if (entity.Id == 0) 
        {
            entity.CreateOnDateTime = DateTime.Now;
            Context.Set<TEntity>().Add(entity);
        }


        Context.SaveChanges(); //this may not suit your design
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读