我不知道我是否遗漏了某些东西……无论如何:
例如,您可以看到具有属性’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'(第一张图片)让我困惑,我一直专注于那……
对不起:-)
解决方法
@H_301_29@ 仍然无法从你的代码中分辨出来,因为你的表现不够.我希望这段代码可以帮助你理解,这对你来说不是一个解决方案,我只是想表明你的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 }