12
  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; }
    }
O.O
  • 11,077
  • 18
  • 94
  • 182

2 Answers2

1

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

Brad Divine
  • 354
  • 2
  • 10
0

try

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

then call

context.SaveChanges()

;)

subseven
  • 89
  • 1
  • 8