-
开发者_开发百科
- Add a new entity to a
TrackableCollection
(context.Entities.Add(entity)
) (EntityState = New) - Without saving, delete the added entity from
TrackableCollection
(context.Entities.Remove(entity)
) (EntityState = Unmodified) - 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; }
}
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
context.Entry(entity).State = EntityState.Detached
then call
context.SaveChanges()
;)
精彩评论