为什么实体消失后仍然有效?
- 将新实体添加到
TrackableCollection
(context.Entities.Add(entity)
) (EntityState = New) - 不保存,从
TrackableCollection
中删除添加的实体code> (context.Entities.Remove(entity)
) (EntityState = 未修改) - 保存。 (
context.SubmitChanges()
)
我仍然从与实体关联的数据注释中收到验证错误,为什么?
public class Entity
{
[Required]
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它正在跟踪已删除实体的集合,即使它没有持久保存到您的存储中(它位于 ObjectsRemovedFromCollection 属性中)。
此链接提供了有关幕后情况的更多信息:MSDN
我没有找到有关显式触发验证的详细信息,但您可以尝试致电在调用 context.SubmitChanges() 之前接受Changes() 或 ObjectsAddedToCollectionProperties.Clear() 和 ObjectsRemovedFromCollectionProperties.Clear()
It is tracking the collection of removed entities, even though it was not persisted to your store (it's in the ObjectsRemovedFromCollection property).
This link has more information about what is going on under the hood: MSDN
I'm not finding details about what explicitly triggers validation, but you can try calling AcceptChanges() or ObjectsAddedToCollectionProperties.Clear() and ObjectsRemovedFromCollectionProperties.Clear() before calling context.SubmitChanges()
尝试
然后打电话
;)
try
then call
;)