【发布时间】:2013-04-04 09:50:45
【问题描述】:
我有两个完全相同的数据库表,一个 prod 和一个 dev。 每当我部署到开发数据库时,一切正常。 prod 数据库允许我尝试使用 EF 和 MVC 将数据写回数据库时抛出此错误。
System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
当然我已经检查了这个http://blogs.msdn.com/b/dsimmons/archive/2010/01/31/deleting-foreign-key-relationships-in-ef4.aspx
似乎我的 EF 代码没有问题,纯粹是 db 表问题 无论如何,我可以找到更多详细信息?
【问题讨论】:
-
请出示这两张表好吗?
-
您是否使用 Red-Gate SQL 比较相同的表,以防您遗漏一些东西?另外,你有没有比较参考表中的数据?
-
我对比了两张表,它们是相同的。
-
好吧,原来是其中之一没有外键。我已经删除了从实体中删除对象的代码。所以现在一切正常
标签: asp.net-mvc entity-framework