c# – 实体框架迁移 – 参照完整性约束违规

c# – 实体框架迁移 – 参照完整性约束违规,第1张

概述我一直在努力解决这个问题,并且无法在Stack Overflow上找到解决方案. 我有一个MVC应用程序,它使用Entity Framework with Repository和Unit of Work模式.我最近从使用网络数据库(Sql Server 2008 R2,问题似乎不存在)转移到本地数据库并开始使用Entity Framework迁移,以便与其他开发人员合作,而不是通过对其进行更改来相 我一直在努力解决这个问题,并且无法在Stack Overflow上找到解决方案.

我有一个MVC应用程序,它使用Entity Framework with Repository和Unit of Work模式.我最近从使用网络数据库(sql Server 2008 R2,问题似乎不存在)转移到本地数据库并开始使用Entity Framework迁移,以便与其他开发人员合作,而不是通过对其进行更改来相互影响我们的模特.

我的模型看起来像这样:

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

每次我尝试更新Student的StudentStatus属性时,我都会遇到以下异常:

“已成功提交对数据库的更改,但更新对象上下文时发生错误.
ObjectContext可能处于不一致状态.内部异常消息:发生参照完整性约束违规:
关系一端的’StudentStatus.ID’的属性值与另一端’Student.StudentStatusID’的属性值不匹配.“

我已经尝试在保存更改之前将导航属性重新设置为null.

student.StudentStatus = null;student.StudentStatusID = 26;_studentRepository.Update(student);_unitOfWork.Commit();

我已经尝试检索特定的StudentStatus对象:

var studentStatus = _studentStatusRepository.GetByID(26);student.StudentStatusID = 26;student.StudentStatus = studentStatus;_studentRepository.Update(student);_unitOfWork.Commit();

但每次尝试它都会在DataContext.SaveChanges()上抛出相同的异常.

我可以毫无问题地更新StudentType(字面意思是同一种关系和类似的类).

Update方法的实现:

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在数据库中创建了更多的索引,但我已经手动删除它们(不是我认为它们一定是问题).我尝试比较两个数据库中的关系,一切似乎都是一样的.

我知道如果没有我提供更多信息可能很难指出可能出现什么问题,但是还有什么可能导致这个问题呢?

编辑(添加了StudentStatus类和相应的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; }    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

我忘了提到我在存储库级别启用了数据库日志记录,以查看Entity Framework正在执行哪些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 ...>,<... 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].
如果添加虚拟StudentStatusType属性,EntityFramework将自动绑定它.

[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; }}
总结

以上是内存溢出为你收集整理的c# – 实体框架迁移 – 参照完整性约束违规全部内容,希望文章能够帮你解决c# – 实体框架迁移 – 参照完整性约束违规所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1231062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存