在 Entity Framework 中 SaveChanges 时,出现下面的错误,请问如何解决?
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.
什么都不修改,直接SaveChanges()时也会出现同样的错误
请提供一下你的数据结构,主键和外键的就可以。
就算是CodeFirst的,你给我看下生成的数据库结构也行。
是实体中的导航属性引起的,去掉所有的virtual关键字后问题就消失
可是把virtual去掉后导航属性不就无效了吗?好像外键也失效了吧
@balahoho: 用fluent API配置关系+Include
@dudu: oh~明白