EF 4.1 Referential integrity error

11,194

Solution 1

Please, try this way

 var surveyRepository = new SurveyRepository();
 foreach (var userAnswer in userAnswers)
 {
+    userAnswer.SurveyId = Survey.Id;
     survey.UserAnswers.Add(userAnswer);
 }
 surveyRepository.InsertOrUpdate(survey);
 surveyRepository.Save();

Solution 2

I know that is late but maybe this could be useful for someone. What about trying this?

var surveyRepository = new SurveyRepository();
surveyRepository.InsertOrUpdate(survey);
foreach (var userAnswer in userAnswers)
{
   survey.UserAnswers.Add(userAnswer);
}
surveyRepository.Save();

Recently I had that "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." error when I was editing an entity with children.

This was my code at the beginning:

myAction.ActionUserNameAssignations.Clear();

string[] sSelectedValues = CheckBoxListHelper.GetAllIds(collection, "CheckBox_UserName_", true).ToArray();
foreach (string userName in sSelectedValues)
{
   ActionUserNameAssignation assignation = new ActionUserNameAssignation { ActionId = myAction.ActionId, UserName = userName };
   myAction.ActionUserNameAssignations.Add(assignation);
}

db.Actions.Attach(myAction);
db.ObjectStateManager.ChangeObjectState(myAction, EntityState.Modified);
db.SaveChanges();

And this is my code at the end, working fine:

db.Actions.Attach(myAction);
db.ObjectStateManager.ChangeObjectState(myAction, EntityState.Modified);

myAction.ActionUserNameAssignations.Clear();

string[] sSelectedValues = CheckBoxListHelper.GetAllIds(collection, "CheckBox_UserName_", true).ToArray();
foreach (string userName in sSelectedValues)
{
   ActionUserNameAssignation assignation = new ActionUserNameAssignation { ActionId = myAction.ActionId, UserName = userName };
   myAction.ActionUserNameAssignations.Add(assignation);
}

db.SaveChanges();

As you can see, the difference is basically Attaching the entity to the context at the beginning. I hope it helps :)

Share:
11,194

Related videos on Youtube

Nealv
Author by

Nealv

I will fill this in later

Updated on May 31, 2022

Comments

  • Nealv
    Nealv almost 2 years

    I have the following classes:

    public class Person
    {
        [Key]
        public Guid Id { get; set; }
    
        [Required]
        public string FirstName { get; set; }
    
        [Required]
        public string LastName { get; set; }
    
        [Required]
        public string Email { get; set; }
    
        public virtual Survey Survey { get; set; }
    }
    
    public class Survey
    {
        [Key]
        public Guid Id { get; set; }
    
        public bool IsFinished { get; set; }
    
        public virtual ICollection<UserAnswer> UserAnswers { get; set; }
    
        public virtual Person Person { get; set; }
    }
    
    public class UserAnswer
    {
        [Key]
        public Guid Id { get; set; }
    
        public Guid SurveyId { get; set; }
        public virtual Survey Survey { get; set; }
    
        public Guid QuestionId { get; set; }
        public virtual Question Question { get; set; }
    
        public Guid AnswerId { get; set; }
        public virtual Answer Answer { get; set; }
     }
    

    In my datacontext I have defined:

     modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional();
     modelBuilder.Entity<Survey>().HasMany(s => s.UserAnswers).WithRequired(a => a.Survey).HasForeignKey(a => a.SurveyId).WillCascadeOnDelete(false);
    

    Can someone tell me what I am doing wrong ?

    Update:

    When I execute this code:

    var surveyRepository = new SurveyRepository();
    foreach (var userAnswer in userAnswers)
    {
        survey.UserAnswers.Add(userAnswer);
    }
    surveyRepository.InsertOrUpdate(survey);
    surveyRepository.Save();
    

    I get the following error:

    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.

  • Nealv
    Nealv almost 13 years
    Nope that's not it, I have some other classes where I used keys and they work fine. I removed the key and still the same error. Thx though for the answer
  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 11 years
    In my case it's a many to many issue. There are Business entity, and Category entity. I want each business to have multiple Categorys, how do I attach them, using their CategoryIDs?
  • Mikael Dúi Bolinder
    Mikael Dúi Bolinder over 5 years
    @Shimmy category.BusinessId = Business.Id or business.Categories.Add(category)?
  • Shimmy Weitzhandler
    Shimmy Weitzhandler over 5 years
    If Business exists and the Id is known to you, use it, otherwise just go with Add, but I think you're covered in any case.