EF 4.1参照完整性错误

时间:2011-05-16 10:10:41

标签: c# .net exception-handling entity-framework-4.1 ef-code-first

我有以下课程:

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

在我的datacontext中,我定义了:

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

有人能告诉我我做错了吗?

更新

执行此代码时:

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

我收到以下错误:

  

参照完整性约束   违规发生:财产   定义参照的值   约束之间不一致   主要和依赖对象   关系。

3 个答案:

答案 0 :(得分:6)

请尝试这种方式

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

答案 1 :(得分:1)

我知道这已经晚了但也许对某人有用。试试这个怎么样?

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

最近我发生了“发生了引用完整性约束违规:定义参照约束的属性值在关系中的主体和从属对象之间不一致。”我在编辑实体时出错和孩子一起

这是我开头的代码:

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

这是我最后的代码,工作正常:

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

正如您所看到的,差异基本上是将实体附加到开头的上下文。我希望它有所帮助:)

答案 2 :(得分:-1)

在定义模型时,您无需指定参考ID属性,EF将在数据库中为您创建这些属性。

将UserAnswer模型更改为

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