Microsoft Entity Framework Core: Cannot insert duplicate key in object

c# entity-framework-core sql

Question

I am trying to update a parent entity, GuildMemberTeam, with child entities, GuildMember, Team and GuildMemberChallenge which also has a child entities, GuildMember and Challenge but am getting the following inner exception:

Inner Exception 1: SqlException: Violation of PRIMARY KEY constraint 'PK_Challenge'. Cannot insert duplicate key in object 'dbo.Challenge'. The duplicate key value is (15ae8798-8567-457b-812a-5820cf7843e5). The statement has been terminated.

The only new entity is the GuildMemberTeam as all the others already exist, but these are checked and recreated as follows:

public void AddChallenge(Challenge challenge)
{
    if (challenge != null)
    {
        var id = challenge.Id == default(Guid) ? Guid.NewGuid() : challenge.Id;

        Challenge = new Challenge(id, challenge.Name, challenge.Phase, challenge.Type, challenge.Star, challenge.Gear, challenge.Level, challenge.Reward);
    }
}

This works for all the other entities apart from Challenge where i get the error. Can anyone please help me understand what i am doing wrong.

1
0
3/8/2019 11:46:41 AM

Accepted Answer

It doesn't change the fact that the problem is that you are trying to insert the same row twice (same Guid=Id) into the dbo.Challenge table.

  1. This might be due to a debugging issue or something. You can either delete the row from the table with a DELETE FROM [Challenge] WHERE Id = '15ae8798-8567-457b-812a-5820cf7843e5' and try running the app again.

  2. If this doesn't solve your problem your entity management is faulty and you have to revise the ID handling. Implement ID checking before you try to save your context or something like that.

The other issue might be that your classes are not defined properly and EF doesn't recognize the relations. The relationships you are talking about are not parent-child, they are either one-to-many, many-to-many, many-to-one or none. DB RELATIONS

Each of your POCO-s should contain and instance of the other class, thus you define a relationship. E.g. if your GuildMemberChallenge contains an IEnumerable and a property with type of challenge.

If none of the above are a solution I need some more code (your classes, the repository) to figure it out.

Update: When you are adding a new GuildMemberChallenge, which I assume you are trying to do now. You should set it's Challenge property to an existing entity if it exists, if it doesn't you can create one, but at the moment you are trying to create a Challenge that already exists in the database.

0
3/8/2019 12:50:12 PM

Popular Answer

You are creating new Challenge but pass id of existing Challenge if it is set.

var id = challenge.Id == default(Guid) ? Guid.NewGuid() : challenge.Id;

I think you, that if you create new entity you should always create new Id

var id = Guid.NewGuid();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow