Entity Framework Update Error

I get a primary key error message with the following code. I can’t understand why this could be, and I need to figure it out. Can anyone help with a fresh pair of eyes?

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();

UPDATE 1: The following is the error I get when calling SaveChanges ():

{"Violation of PRIMARY KEY constraint 'PK_Matches_1'. Cannot insert duplicate key in object 'dbo.Matches'.\r\nThe statement has been terminated."}

UPDATE 2: I do not use the identity insert in the database table for this, since this is an import from a third-party web service, where I need to save all identifiers. I'm not sure if this will affect the upgrade process with entity infrastructure?

UPDATE 3: Well, when I enable the identity insert, the update was successful, but I do not want to have an indentity insert in this table, since the identifiers are passed from the WebService, and I need to save these identifiers.

+3
2

, 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:

+2

, , Event_ID. , match DBContext, Event_ID, -.

if (theEvent.Last_Updated < matchEvent.Last_Updated) 
{
  //Update the current event
  theEvent.Event_ID = matchEvent.Event_ID;  // <-- Delete this line.
  ...
}

, , ? , DBContext . . Event_ID, , .

, .

0

Source: https://habr.com/ru/post/1774386/


All Articles