EF 4.1 Referential integrity error

99封情书 提交于 2019-12-08 15:27:13

问题


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.


回答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();



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




回答3:


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; }
} 


来源:https://stackoverflow.com/questions/6015843/ef-4-1-referential-integrity-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!