开发者

EF4: OfType filter for GetObjectStateEntries

开发者 https://www.devze.com 2023-02-25 18:20 出处:网络
I override the SaveChanges method in my datacontext and have the following code: IEnum开发者_运维百科erable<BaseEntity> newEntries = ObjectStateManager.GetObjectStateEntries(EntityState.Added).

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

精彩评论

暂无评论...
验证码 换一张
取 消