SaveChanges() is saving Unchanged records

Viewed 240

I don't know if I'm missing something... anyway:

image 1

You can see, for example, that the object with the property 'HomeTeam' = ' Forest Green Rovers' has the state = 'Unchanged'. Anyway all entities are 'unchanged' in my case. So, if I'm correct, the saveChanges shouldn't try to insert those in my table but that's what it's happening:

image 2

The primary key has been violated, but EF shouldn't have tried to add this record (the one with the property HomeTeam = 'Forest Green Rovers') since it's 'Unchanged', right?

Why is EF doing that?

The entity:

{
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; }
}

}

More Info:

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 */

ops. I have found my error and it's really stupid :-( in a related function inside the for there was some uncommented code where I was adding entities without checking the PK constraints. The state 'Unchanged' (first image) confused me and I have kept focusing on that... Sorry :-)

4 Answers
Related