我已经为此苦苦挣扎了一段时间,在堆栈溢出上找不到解决方案。
我有一个MVC应用程序,它使用带有存储库和工作单元模式的实体框架。我最近从使用网络数据库(Sql Server 2008 R2,问题似乎不存在)转移到本地数据库,并开始使用实体框架迁移,以便与其他开发人员合作,并且不会通过更改模型来相互影响。
我的模型如下所示:
[Table("Student")]
public class Student
{
[Key]
public int Id { get; set; }
<... other fields ...>
[Required(ErrorMessage = "A student type is required")]
public int StudentTypeId { get; set; }
[Required(ErrorMessage = "A student status is required")]
public int StudentStatusId { get; set; }
[ForeignKey("StudentTypeId")]
public virtual StudentType StudentType { get; set; }
[ForeignKey("StudentStatusId")]
public virtual StudentStatus StudentStatus { get; set; }
}
每次我尝试更新学生的学生状态属性时,我都会收到以下异常:
"对数据库的更改已成功提交,但在更新对象上下文时发生错误。对象上下文可能处于不一致的状态。内部异常消息:发生参照完整性约束冲突:关系一端的'StudentStatus.Id'属性值与另一端的'Student.StudentStatusId'的属性值不匹配。
在保存更改之前,我尝试将导航属性重新设置为 null。
student.StudentStatus = null;
student.StudentStatusId = 26;
_studentRepository.Update(student);
_unitOfWork.Commit();
我尝试检索特定的学生状态对象:
var studentStatus = _studentStatusRepository.GetById(26);
student.StudentStatusId = 26;
student.StudentStatus = studentStatus;
_studentRepository.Update(student);
_unitOfWork.Commit();
但每次尝试都会在 DataContext.SaveChanges() 上抛出相同的异常。
我可以毫无问题地更新 StudentType(实际上是相同类型的关系和类似的类)。
更新方法的实现:
public virtual void Update(T entity)
{
try
{
DataContext.Entry(entity).State = EntityState.Modified;
}
catch (Exception exception)
{
throw new EntityException(string.Format("Failed to update entity '{0}'", typeof(T).Name), exception);
}
}
我注意到,与我以前的网络数据库相比,EF Migrations 在数据库中创建了更多索引,但此后我手动删除了它们(并不是我认为它们一定是问题所在)。我尝试比较两个数据库中的关系,一切似乎都是一样的。
我知道如果没有我提供更多信息,可能很难指出可能出现的问题,但是还有什么可能导致此问题?
编辑(添加了学生状态类和相应的学生状态类型)
[Table("StudentStatus")]
public class StudentStatus
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Student status name is required")]
[MaxLength(50, ErrorMessage = "Student status name cannot be longer than 50 characters")]
public string Name { get; set; }
public int StudentStatusTypeId { get; set; }
[ForeignKey("StudentStatusTypeId")]
public virtual StudentStatusType StudentStatusType { get; set; }
}
[Table("StudentStatusType")]
public class StudentStatusType
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
编辑 2
我忘了提到我在存储库级别启用了数据库日志记录,以查看实体框架正在执行哪些 SQL 查询:
DataContext.Database.Log = s => Debug.WriteLine(s);
结果是:
UPDATE [dbo].[Student]
SET <... some parameters ...>, [StudentStatusId] = @10, <... some parameters ...>
WHERE ([Id] = @14)
UPDATE [dbo].[Student]
SET <... some parameters ...>, [StudentStatusId] = @10, <... some parameters ...>
WHERE ([Id] = @14)
<... some parameters ...>
-- @10: '25' (Type = Int32)
-- @10: '25' (Type = Int32)
<... some parameters ...>
-- Executing at 12/01/2015 12:30:41 +00:00
-- Executing at 12/01/2015 12:30:41 +00:00
-- Completed in 0 ms with result: 1
-- Completed in 0 ms with result: 1
那么,为什么 EF 尝试插入值 25,即使我明确将其指定为 26?这是否导致了根本问题?另外,为什么会有两个更新语句而不是一个?
对我来说,这种方法似乎更直观:
int StudentStatusType
保存 StudentStatusType.Id
的值,所以应该标记为 [ForeignKey]
。如果添加 virtual StudentStatusType
-属性,实体框架将自动绑定它。
[Table("StudentStatus")]
public class StudentStatus
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Student status name is required")]
[MaxLength(50, ErrorMessage = "Student status name cannot be longer than 50 characters")]
public string Name { get; set; }
[ForeignKey("StudentStatusType")]
public int StudentStatusTypeId { get; set; }
public virtual StudentStatusType StudentStatusType { get; set; }
}
[Table("StudentStatusType")]
public class StudentStatusType
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
可能会对 on 的导航属性感到困惑StudentStatus
与Student
完全无关。将以下行添加到 StudentStatus
类应该可以解决问题。
public virtual ICollection<Student> Students { get; set; }
在Adimeus的建议下,我不得不调查初始数据是如何播种的。
与 StudentType 相反,StudentStatus 是在我的服务层(而不是 EF Migrations 的 Configuration.cs 文件中播种的 - 不要问我为什么;这是另一个处理迁移的开发人员。
是:
if (!_studentStatusRepository.GetAll().Any())
{
var newStudentStatus = _studentStatusTypeRepository.Get(x => x.Name == "New");
var activeStudentStatus = _studentStatusTypeRepository.Get(x => x.Name == "Active");
var deletedStudentStatus = _studentStatusTypeRepository.Get(x => x.Name == "Deleted");
var studentStatuses = new List<StudentStatus>
{
new StudentStatus {Name = "New", StudentStatusType = newStudentStatus, StudentStatusTypeId = newStudentStatus.Id},
new StudentStatus {Name = "Awaiting Approval", StudentStatusType = activeStudentStatus, StudentStatusTypeId = activeStudentStatus.Id},
new StudentStatus {Name = "Approved", StudentStatusType = activeStudentStatus, StudentStatusTypeId = activeStudentStatus.Id},
new StudentStatus {Name = "Deleted", StudentStatusType = deletedStudentStatus, StudentStatusTypeId = deletedStudentStatus.Id},
new StudentStatus {Name = "Reinstated", StudentStatusType = deletedStudentStatus, StudentStatusTypeId = deletedStudentStatus.Id}
};
foreach (var studentStatus in studentStatuses.ToList())
{
StudentStatus status = studentStatus;
var dbStudentStatus = _studentStatusRepository.Get(x => x.Name == status.Name);
if (dbStudentStatus == null)
{
_studentStatusRepository.Add(studentStatus);
}
}
_unitOfWork.Commit();
}
我已将种子设定移动到EF迁移配置.cs文件(我想它可能可以优化,但这是一个快速测试):
var studentStatuses = new List<StudentStatus>
{
new StudentStatus
{
Name = "New",
StudentStatusType = context.StudentStatusTypes.Single(x => x.Name == "New"),
StudentStatusTypeId = context.StudentStatusTypes.Single(x => x.Name == "New").Id,
},
new StudentStatus
{
Name = "Awaiting Approval",
StudentStatusType = context.StudentStatusTypes.Single(x => x.Name == "Active"),
StudentStatusTypeId = context.StudentStatusTypes.Single(x => x.Name == "Active").Id,
},
new StudentStatus
{
Name = "Approved",
StudentStatusType = context.StudentStatusTypes.Single(x => x.Name == "Active"),
StudentStatusTypeId = context.StudentStatusTypes.Single(x => x.Name == "Active").Id,
},
new StudentStatus
{
Name = "Deleted",
StudentStatusType = context.StudentStatusTypes.Single(x => x.Name == "Deleted"),
StudentStatusTypeId = context.StudentStatusTypes.Single(x => x.Name == "Deleted").Id,
},
new StudentStatus
{
Name = "Reinstated",
StudentStatusType = context.StudentStatusTypes.Single(x => x.Name == "Deleted"),
StudentStatusTypeId = context.StudentStatusTypes.Single(x => x.Name == "Deleted").Id,
}
};
studentStatuses.ForEach(x => context.StudentStatuses.AddOrUpdate(y => y.Name, x));
问题消失了!我现在可以更新学生状态。不太明白为什么服务中的种子首先会导致问题。如果有人能解释,我会接受答案;否则我会在几天后接受我的答案。