I override the SaveChanges method in my datacontext and have the following code:
IEnum开发者_运维百科erable<BaseEntity> newEntries = ObjectStateManager.GetObjectStateEntries(EntityState.Added).OfType<BaseEntity>();
In my model all businessobjects derive from BaseEntity. When I add a new 'Investment' (also derived from BaseEntity!) to my datacontext the code above will not show this entity in the resultset.
GetObjectStateEntries(EntityState.Added)
however returns one entity which is of type Investment
.
Why doesn't OfType<>
recognize if an object is derived?
That because this query returns objects of type ObjectStateEntry
and instance of this object has a property Entity
which holds your entity object.
Try this:
var IEnumerable<BaseEntity> newEntries = ObjectStateManager
.GetObjectStateEntries(EntityState.Added)
.Select(e => e.Entity)
.OfType<BaseEntity>();
精彩评论