Referential Integrity Constraint violation when attempting to set a FK to null
Asked Answered
K

1

10

I am trying to update an entity in EF6. I have read that if I wish to change a ForeignKey property, I have to then ensure the Navigation Property is the correct one, or set it to null.

I have taken the set to null approach, but I still receive the Referential Integrity Constraint Exception:

A referential integrity constraint violation occurred: The property value(s) of 'Contact.idContact' on one end of a relationship do not match the property value(s) of 'Entity.id_EntityContactInfo' on the other end.

But you can see in the debugger, that Entity.Contact is null, so I believe this shouldn't be throwing.

enter image description here

Any Ideas?

EDIT

This is how the entity is updated:

public T CommitUpdate<T>(T obj) where T : class
    {
        _DbContext.Set<T>().Attach(obj);
        _DbContext.Entry(obj).State = EntityState.Modified;
        _DbContext.Commit();
        return obj;
    }
Korwin answered 15/8, 2014 at 2:6 Comment(9)
Can you try to set the FK id value to null?Immigrant
But I need it to be the ID, and the compiler wont allow it as its Non-NullKorwin
You can just change the id value without updating the nav property.Immigrant
If I change the ID value to the one I want, I get the exception. If I change the ID value to the one I want and set the Nav Property to null, I get the exception.Korwin
You might try detaching, updating, then reattaching to see if it's a problem in the object graph.Immigrant
I have just added some code with regards to the updating of the entityKorwin
So you have FK id value which is non nullable and you have FK property which you have set to null? That means the Contact is required, right? Can you check whether the FK column in the database is nullable or not ?Shellbark
Ah ok, so this brings me back to my issue yesterday, I want to change the FK scalar, but not add the current item into the database again... I guess there is no generic way to do that without setting all Navigation Properties to EntityState.UnChangedKorwin
Perhaps it wants to set the FK id in the database as 0, but can't find contact which has id = 0Shellbark
S
10

From what I see from comments, you are solving this problem:

I want to change the FK scalar, but not add the current item into the database again

You should have mapping something like this one:

public class MyEntity {
    ...

    public int ContactId { get; set; }

    [ForeignKey("ContactId")]
    public Contact Contact { get; set; }
    ...
}

As FK is declared as not-nullable, you have to set it.

Basically you have several options to do it:

  • Set ContactId to real ID in database, set Contact to null

    In this case, you will update FK with existing Contact in DB - hopefully the option you need.

  • Set ContactId to 0, and set Contact to new Contact(..)

    In this case, EF will try to create new Contact in DB first, and then will use its PK to feed ContactId FK.

  • Create empty Contact entity, set its ID to existing contact ID. Then, use this entity as Contact field for your entity in question. Then, attach that Contact to context with UnChanged state.

    Having this, you will say to EF that this Contact is already existing, should not be tracked and should not be changed, but its ID will be used as FK for your parent entity. Just take care that this Contact should be attached (in unchanged state) to the same context as its parent.

Stele answered 15/8, 2014 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.