Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 Referential integrity error

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.

like image 313
Nealv Avatar asked May 16 '11 10:05

Nealv


2 Answers

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();
like image 77
Davi Fiamenghi Avatar answered Oct 05 '22 23:10

Davi Fiamenghi


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 :)

like image 44
Alfonso Muñoz Avatar answered Oct 05 '22 22:10

Alfonso Muñoz