使用 AsNoTracking 验证失败
我正在尝试在我的上下文中添加一个新实体,如下所示:
article.Id = 1;
var rede = from r in mycontext.Redevable.AsNoTracking()
where r.Matricule == "0001414"
select r;
article.Redevable = rede.First<Redevable>();
...
mycontext.Article.Add(article);
mycontext.SaveChanges();
我收到一个 DbEntityValidationException 异常,表示 Redevable 是强制性的,因为属性“Redevable”被标记为“必需”。
如果我删除“AsNoTracking”,它工作正常,但性能非常糟糕。
你能帮我吗?
提前致谢。
i'm trying to add a new entity in my context like that :
article.Id = 1;
var rede = from r in mycontext.Redevable.AsNoTracking()
where r.Matricule == "0001414"
select r;
article.Redevable = rede.First<Redevable>();
...
mycontext.Article.Add(article);
mycontext.SaveChanges();
and i get a DbEntityValidationException saying that the Redevable is mandatory because the property "Redevable" is marker as Required.
It works fine if i remove "AsNoTracking" but the performance are very bad.
Could you help me ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以仅使用 Id 创建 Fk 属性,如下所示:
然后从 Redevable 实例设置它:
然后保留您的更改:
You can create a Fk Property only with the Id like this:
and then set it from the Redevable instance:
then persist your changes:
AsNoTracking() 意味着您无法更新这些实体而不将它们重新附加到跟踪图。有关更多详细信息请参阅
AsNoTracking() means that you cant update these entities without reattaching them to the tracking graph. For more details see