Request attached objects

I do a few insert commands before running SaveChanges.

Is there a way to request attached objects (which I inserted right now before SaveChanges) to check if a particular record has been added or updated?

+3
source share
1 answer

Yes, there is a way. ObjectContextThe instance offers a property called ObjectStateManger. ObjectStateManagermanages all attached objects and knows its state:

ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(attachedEntity);
EntityState state = entry.State;

If you need to get all the modified or added objects that you can use:

var entities = context.ObjectStateManager
                      .GetObjectStateEntries(EntityState.Added | EntitiSate.Modified)
                      .Select(e => e.Entity);

OfType - . SaveChanges, , .

+2

All Articles