Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is entity still validated when it is gone?

  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; }
    }
like image 886
O.O Avatar asked Jun 06 '11 20:06

O.O


1 Answers

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()

like image 133
Brad Divine Avatar answered Oct 09 '22 10:10

Brad Divine