Entity Framework: Re-finding objects recently added to context

前端 未结 7 694
感情败类
感情败类 2020-11-30 03:53

I am using the entity framework and I\'m having a problem with \"re-finding\" objects I just created... basically it goes like this:

string theId = \"someId         


        
7条回答
  •  醉梦人生
    2020-11-30 04:07

    I was in the same situation. I wrote this extension method that at least for me solves the problem (I don't have issues with i.e conflicts in my context...)

        public static IEnumerable WhereInclAdded(this ObjectSet set, Expression> predicate)  where T : class
        {
            var dbResult = set.Where(predicate);
    
            var offlineResult = set.Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Select(entry => entry.Entity).OfType().Where(predicate.Compile());
    
            return offlineResult.Union(dbResult);
        }
    

提交回复
热议问题