Is DbSet<>.Local something to use with special care?

后端 未结 3 1088
忘掉有多难
忘掉有多难 2021-02-05 14:35

For a few days now, I have been struggling with retrieving my entities from a repository (DbContext).

I am trying to save all the entities in an atomic act

3条回答
  •  执念已碎
    2021-02-05 15:12

    As mentioned by Terry Coatta, the best approach if you don't want to save the records first would be checking both sources.

    For example:

    public Person LookupPerson(string emailAddress, DateTime effectiveDate)
    {
        Expression> criteria = 
            p =>
                p.EmailAddress == emailAddress &&
                p.EffectiveDate == effectiveDate;
    
        return LookupPerson(_context.ObjectSet.Local.AsQueryable(), criteria) ?? // Search local
               LookupPerson(_context.ObjectSet.AsQueryable(), criteria); // Search database
    }
    
    private Person LookupPerson(IQueryable source, Expression> predicate)
    {
        return source.FirstOrDefault(predicate);
    }
    

提交回复
热议问题