Note that there are some explanatory texts on larger screens.

plurals
  1. POEF 5, update object gives "A referential integrity constraint violation occurred"
    primarykey
    data
    text
    <p>In my model I've got a bunch of domain objects. Now I'm having a problem when trying to update a User-object. The User has a foreignkey relation to the Role object. When I update the User-object without changeing the foreignkey value (FkRoleId) it all works fine. But when I change the role for the current user I want to update I get the error: </p> <blockquote> <p>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.</p> </blockquote> <p>Here's how I update my user-object:</p> <pre><code>public void Update(User user) { using (var context = new DBEntities()) { context.Entry(user).State = System.Data.EntityState.Modified; context.SaveChanges(); } } </code></pre> <p>How can I update my user-object without getting this exception? There must be a way for chaning the foreignkey value for mappen the user to another role. </p> <p>Here's my domain classes in this case:</p> <pre><code>public partial class User { public User() { this.Advertisers = new HashSet&lt;Advertiser&gt;(); this.Cases = new HashSet&lt;Case&gt;(); this.Materials = new HashSet&lt;Material&gt;(); } public int PkId { get; set; } public int FkRoleId { get; set; } public string Name { get; set; } public string Email { get; set; } public string Password { get; set; } public System.DateTime Created { get; set; } public bool Active { get; set; } public virtual ICollection&lt;Advertiser&gt; Advertisers { get; set; } public virtual ICollection&lt;Case&gt; Cases { get; set; } public virtual ICollection&lt;Material&gt; Materials { get; set; } public virtual Role Role { get; set; } } public partial class Role { public Role() { this.Users = new HashSet&lt;User&gt;(); } public int PkId { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection&lt;User&gt; Users { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload