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

Mas*_*imo 6 c# entity-framework

我不知道我是否遗漏了某些东西......无论如何:

图片1

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

图片2

主键已被违反,但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; }
}
Run Code Online (Sandbox Code Playgroud)

}

更多信息:

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 */
Run Code Online (Sandbox Code Playgroud)

欢声笑语.我发现了我的错误,这真的很愚蠢:-(在一个相关的函数里面,因为有一些未注释的代码,我在没有检查PK约束的情况下添加实体.状态'Unchanged'(第一张图片)让我困惑,我保持专注于那...抱歉:-)

Sea*_*kit 1

仍然无法从您的代码中看出,因为您的显示不够。

您需要包含更多代码,以便我们了解您为何调用您的名称。

我希望这段代码可以帮助您理解,这不是适合您的解决方案,我只是想表明您的 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
    }
Run Code Online (Sandbox Code Playgroud)