public TEntity Find(params object[] keyValues)//传入值,可以为多个值,联合主键
{
this.InternalContext.ObjectContext.AsyncMonitor.EnsureNotEntered();
this.InternalContext.DetectChanges(false);
WrappedEntityKey key = new WrappedEntityKey(this.EntitySet, this.EntitySetName, keyValues, "keyValues");
object obj2 = this.FindInStateManager(key) ?? this.FindInStore(key, "keyValues");//FindInStateManager在缓存中查找 FindInStore在数据库中查找
if ((obj2 != null) && !(obj2 is TEntity))//如果仍然找不到,就抛出异常,否则返回对应实体。
{
throw System.Data.Entity.Resources.Error.DbSet_WrongEntityTypeFound(obj2.GetType().Name, typeof(TEntity).Name);
}
return (TEntity) obj2;
}
EF Find()源码
最新推荐文章于 2024-04-18 16:09:20 发布