首页 新闻 会员 周边

Entity Framework提交时外键为空的问题

0
悬赏园豆:20 [待解决问题]

在 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.

EF
问题补充:

什么都不修改,直接SaveChanges()时也会出现同样的错误

dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2016-10-08 12:12
< >
分享
所有回答(2)
0

请提供一下你的数据结构,主键和外键的就可以。

就算是CodeFirst的,你给我看下生成的数据库结构也行。

爱编程的大叔 | 园豆:30839 (高人七级) | 2016-10-08 12:40
0

是实体中的导航属性引起的,去掉所有的virtual关键字后问题就消失

dudu | 园豆:31003 (高人七级) | 2016-10-08 13:04

可是把virtual去掉后导航属性不就无效了吗?好像外键也失效了吧

支持(0) 反对(0) balahoho | 园豆:2050 (老鸟四级) | 2016-10-09 11:59

@balahoho: 用fluent API配置关系+Include

支持(0) 反对(0) dudu | 园豆:31003 (高人七级) | 2016-10-09 16:21

@dudu: oh~明白

支持(0) 反对(0) balahoho | 园豆:2050 (老鸟四级) | 2016-10-09 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册