开发者

EF4: OfType filter for GetObjectStateEntries

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜