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

问题描述

| 将新实体添加到
TrackableCollection
context.Entities.Add(entity)
)(EntityState = New) 不保存,从
TrackableCollection
context.Entities.Remove(entity)
)中删除添加的实体(EntityState =未修改) 保存。 (
context.SubmitChanges()
) 我仍然从与实体关联的数据注释中获得验证错误,为什么?
    public class Entity
    {
       [Required]
       public string Name { get; set; }
    }
    

解决方法

        即使没有持久存储在商店中(它位于ObjectsRemovedFromCollection属性中),它也会跟踪已删除实体的集合。 此链接提供了有关幕后情况的更多信息:MSDN 我没有找到有关显式触发验证的详细信息,但是您可以在调用context.SubmitChanges()之前尝试调用AcceptChanges()或ObjectwegoToCollectionProperties.Clear()和ObjectsRemovedFromCollectionProperties.Clear()。     ,        尝试
context.Entry(entity).State = EntityState.Detached
然后打电话
context.SaveChanges()
;)     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...