gpt4 book ai didi

c# - SaveChanges() 正在保存 Unchanged 记录

转载 作者:太空狗 更新时间:2023-10-29 21:16:21 25 4
gpt4 key购买 nike

我不知道我是否遗漏了什么......无论如何:

image 1

例如,您可以看到属性为“HomeTeam”=“Forest Green Rovers”的对象的状态为“Unchanged”。无论如何,就我而言,所有实体都是“不变的”。所以,如果我是正确的,saveChanges 不应该尝试将它们插入我的表中,但这就是它正在发生的事情:

image 2

违反了主键,但 EF 不应该尝试添加此记录(具有属性 HomeTeam = 'Forest Green Rovers' 的记录),因为它是“未更改”,对吗?

为什么 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 */

操作。我发现了我的错误,它真的很愚蠢 :-( 在 for 的相关函数中有一些未注释的代码,我在其中添加实体而不检查 PK 约束。“未更改”状态(第一张图片)让我感到困惑,我一直专注于此......对不起:-)

最佳答案

如果您想向上下文中添加不是新实体的实体(PK 已存在于数据库中),那么您需要附加实体而不是添加实体。

此处有更多详细信息 https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx

要详细说明,请仔细查看上下文内容。您会发现一个具有已添加状态的实体。

关于c# - SaveChanges() 正在保存 Unchanged 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com