none
A referential integrity constraint violation occurred

    Question

  • I'm using Entity Framework v1 and VS2008 .I have two entities one represent the AspNet_User and the second represent a table I made MyUserSettingsEntityTable which has the UserId as a primary key for itself and as a foriegn key for the AspNet_User in the same time.

    When running the code come to updating MyUserSettingsEntity I got the following error :

     A referential integrity constraint violation occurred: A property that is a part of referential integrity constraint cannot be changed when the object has a non-temporary key.

    After attaching the  AspNet_User entity to the context its entry state become UnChanged is this OK ?

    However I hope to solve this error I realy don't know what to do ?

    Here is the code I'm using for updating MyUserSettingsEntity :

    public void Update(MyUserSettingsEntity userSetting)
    
    
    
     {
    
    
    
     if (userSetting.EntityState == EntityState.Detached)
    
    
    
     {
    
    
    
     object userSetting1 = null;
    
    
    
     if (Projectctx.TryGetObjectByKey(userSetting.EntityKey, out userSetting1))
    
    
    
     
    
    
    
     {
    
    
    
     Projectctx.ApplyPropertyChanges("UserSettings", userSetting);
    
    
    
     Projectctx.ApplyReferencePropertyChanges((IEntityWithRelationships)userSetting, (IEntityWithRelationships)userSetting1);
    
    
    
     }
    
    
    
     }
    
    
    
     Projectctx.SaveChanges();
    
    
    
     }
    
    
    
    
    
    
    
    In the Context Class :
    
    
    
    
    
    
    
    public void ApplyReferencePropertyChanges( 
    
    
    
    IEntityWithRelationships newEntity, 
    
    
    
    IEntityWithRelationships oldEntity) 
    
    
    
    
    
    
    
    {
    
    
    
    foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds()) 
    
    
    
    {
    
    
    
    var oldRef = relatedEnd as EntityReference; 
    
    
    
    if (oldRef != null) 
    
    
    
    {
    
    
    
    var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference; 
    
    
    
    
    
    
    
    oldRef.EntityKey = newRef.EntityKey;
    
    
    
    }
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    
     
    
    
    
    

    When the error occurs the Invalid Operation Exception points to the line :

    oldRef.EntityKey = newRef.EntityKey;

     


    I am too late but i will never give up
    • Edited by The Eagle Saturday, August 14, 2010 3:50 AM Correct the question text
    Saturday, August 07, 2010 7:41 AM

Answers

  • Never mind I solved my problem after analysing my code multiple times.I found the problem in another code paragraph...


    I am too late but i will never give up
    • Marked as answer by Roahn Luo Monday, August 30, 2010 11:21 AM
    Friday, August 27, 2010 9:56 AM

All replies

  • Please any suggestion could be helpfull...
    I am too late but i will never give up
    Wednesday, August 18, 2010 1:14 AM
  • Never mind I solved my problem after analysing my code multiple times.I found the problem in another code paragraph...


    I am too late but i will never give up
    • Marked as answer by Roahn Luo Monday, August 30, 2010 11:21 AM
    Friday, August 27, 2010 9:56 AM