为什么实体消失后仍然有效?

发布于 2024-11-14 16:48:24 字数 463 浏览 7 评论 0 原文

  1. 将新实体添加到 TrackableCollection (context.Entities.Add(entity)) (EntityState = New)
  2. 不保存,从 TrackableCollection 中删除添加的实体code> (context.Entities.Remove(entity)) (EntityState = 未修改)
  3. 保存。 (context.SubmitChanges())

我仍然从与实体关联的数据注释中收到验证错误,为什么?

    public class Entity
    {
       [Required]
       public string Name { get; set; }
    }
  1. Add a new entity to a TrackableCollection (context.Entities.Add(entity)) (EntityState = New)
  2. Without saving, delete the added entity from TrackableCollection (context.Entities.Remove(entity)) (EntityState = Unmodified)
  3. Save. (context.SubmitChanges())

I still get validation errors from the data annotations associated with the entity, why?

    public class Entity
    {
       [Required]
       public string Name { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-11-21 16:48:24

它正在跟踪已删除实体的集合,即使它没有持久保存到您的存储中(它位于 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()

聆听风音 2024-11-21 16:48:24

尝试

context.Entry(entity).State = EntityState.Detached

然后打电话

context.SaveChanges()

;)

try

context.Entry(entity).State = EntityState.Detached

then call

context.SaveChanges()

;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文