11
votes

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.

3
What error did you get and when? - Ladislav Mrnka

3 Answers

6
votes

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();
1
votes

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

-1
votes

When defining your model, you don't need to specify the Referential Id properties EF will create these in the database for you.

Change your UserAnswer model to

public class UserAnswer
{
    [Key]
    public Guid Id { get; set; }

    public virtual Survey Survey { get; set; }

    public virtual Question Question { get; set; }

    public virtual Answer Answer { get; set; }
}