none
referential integrity constraint violation during Edit RRS feed

  • Question

  • I have a class that includes reference to another class, say

    public class Person

    {

    public int id { get; set;}

    public string name { get; set; }

    public int addressId { get; set; } // fk

    public Address address { get; set; } // nav prop

    }

    public class Address

    {

    public int id { get; set; }

    public string address { get; set; }

    }

    I like to be able to add/edit the address while editing the person. The add and save works. But when I post the edit, it threw an exception at db.Entry(person).State = EntityState.Modified;

    The error message was

    "A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship"

    Do I need to add fluent API to clarify the relationship between Person and Address class?

    Daniel

    Monday, March 10, 2014 4:55 PM

Answers

  • Hi Fred,

    I solved the problem. I found that I had to do 2 more things at HttpPost:

    1) update the Address object that EF created with addressId = Person.addressId

    2) mark db.entry(person.Address).State = EntityState.Modified

    then I can save and update the edited Person and associated Address.

    Thanks for your help.

    Daniel

    • Marked as answer by Daniel Jax Tuesday, March 11, 2014 6:58 PM
    Tuesday, March 11, 2014 6:58 PM

All replies

  • Hello Daniel Jax,

    If I understand correctly, you are using Entity Framework Code First.

    >>Do I need to add fluent API to clarify the relationship between Person and Address class?

    Yes, it is recommended to use the fluent API to configure relationships between entities.

    What is the relationship between Person and Address? If it is one to one, it also needs the Person type in Address:

    public Person person { get; set; }

    If it is one to many, it needs a collection of Person in Addresss:

    public ICollection<Person> person { get; set; }

    For details, you can check below link:

    http://msdn.microsoft.com/en-us/data/jj591620.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 11, 2014 5:37 AM
    Moderator
  • Hi Fred Bao,

    Thanks for your suggestion. I added the property Person in Address class as suggested and tried the following API

    modelBuilder.Entity<Person>().HasRequired( p => p.Address);

    modelBuilder.Entity<Address>().HasRequired(a => a.Person);

    When I add-migration, it complained

    Unable to determine the principal end of an association between the types 'IGRT.Entities.TxField' and 'IGRT.Entities.PosVecToler'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

    I obviously still confused with fluent API.

    Daniel

    Tuesday, March 11, 2014 2:00 PM
  • Hi Fred,

    The error message was straight from my app. The TxField and PosVecToler classes would translate to Person/Address in my question.

    Daniel

    Tuesday, March 11, 2014 3:10 PM
  • Hi Fred,

    I solved the problem. I found that I had to do 2 more things at HttpPost:

    1) update the Address object that EF created with addressId = Person.addressId

    2) mark db.entry(person.Address).State = EntityState.Modified

    then I can save and update the edited Person and associated Address.

    Thanks for your help.

    Daniel

    • Marked as answer by Daniel Jax Tuesday, March 11, 2014 6:58 PM
    Tuesday, March 11, 2014 6:58 PM