c# - Entity Framework 更新错误

标签 c# entity-framework entity

我收到以下代码的主键错误消息。我看不出为什么会这样,我需要解决这个问题。谁能帮忙换一双新眼睛?

var events = (from e in nodes.Descendants("event")
                              select new Event
                              {
                                  Event_ID = int.Parse(e.Attribute("event_id").Value),
                                  Name = e.Attribute("name").Value,
                                  Code = e.Attribute("code").Value,
                                  Minute = e.Attribute("minute").Value != String.Empty ? int.Parse(e.Attribute("minute").Value) : 0,
                                  Minute_Extra = e.Attribute("minute_extra").Value != String.Empty ? int.Parse(e.Attribute("minute_extra").Value) : 0,
                                  Team = GetTeam(e.Attribute("team_id")),
                                  Last_Updated = DateTime.Parse((FormatDateTime(e.Attribute("last_updated").Value)))

                              });

foreach (Event matchEvent in events)
{

    //Check to see if this event exists
    if (match.Events.Any(o => o.Event_ID == matchEvent.Event_ID))
    {
        Event theEvent = (from m in match.Events
                          where m.Event_ID == matchEvent.Event_ID
                          select m).FirstOrDefault();

        //There is an event with this ID, so check its last updated flag
        if (theEvent.Last_Updated < matchEvent.Last_Updated)
        {
            //Update the current event
            theEvent.Event_ID = matchEvent.Event_ID;
            theEvent.Name = matchEvent.Name;
            theEvent.Code = matchEvent.Code;
            theEvent.Minute = matchEvent.Minute;
            theEvent.Minute_Extra = matchEvent.Minute_Extra;
            theEvent.Team = matchEvent.Team;
            theEvent.Last_Updated = matchEvent.Last_Updated;
        }

    }
    //If the event is not there we need to add it
    else
    {
        match.Events.Add(matchEvent);
    }

    myDb.SaveChanges();

更新 1:以下是调用 SaveChanges() 时出现的错误:

{“违反 PRIMARY KEY 约束 'PK_Matches_1'。无法在对象 'dbo.Matches' 中插入重复键。\r\n语句已终止。”

更新 2:我没有在数据库表上使用标识插入,因为这是从第 3 方 Web 服务导入的,我需要在其中保留所有 ID。我不确定这是否会影响 Entity Framework 的更新过程?

更新 3:好的,当我打开身份插入时,更新成功,但是我不希望在此表上插入身份,因为 ID 是从 Web 服务传入的,我需要保留这些 ID。

最佳答案

我不确定,因为我对 Entity Framework 不太感兴趣,但是你需要这一行吗?

theEvent.Event_ID = matchEvent.Event_ID;

紧接着

//There is an event with this ID, so check its last updated flag
if (theEvent.Last_Updated < matchEvent.Last_Updated)

而且我认为这是多余的,并且还可能导致主键错误,因为我认为您不能在主键创建后分配给它。

更新

快速搜索了一下,发现主键创建后无法更新,所以我很确定这就是您的错误所在。

请参阅此 SO 答案:Update primary key value using entity framework

关于c# - Entity Framework 更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158731/

相关文章:

c# - 无法向 .NET Core 中的 IConfiguration 添加扩展方法(但在其他类上工作)

C# OO 设计问题

mysql - 将 MySql Provider 与 Entity Framework 模型结合使用

c# - Entity Framework 代码首次日期字段创建

doctrine-orm - 原则 2 中没有为实体指定标识符/主键

java - 在 Hibernate 中处理环境特定字段的最佳方法

c# - 如何解决 "' UnityEngine.Random' does not contain a definition for 'Next' ...”错误?

c# - 在 docker 机器上调试时 MySql 连接被拒绝

asp.net-mvc - 如何使用 EF 构建 ASP.Net MVC 应用程序?

java - jpa中@ColumnDefault的值字段是什么?