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)
{
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();
if (theEvent.Last_Updated < matchEvent.Last_Updated)
{
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;
}
}
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.