Entity Framework API介绍 -- DbSet<>().Find()

过去我们常常使用Where或First(FirstOrDefault)方法来查找对应的实体,比如:

  1. var query = context.CertInfoMakeDetails.ToList().Where(make => int.Parse(make.CertCode) >= startcode && 
  2.                                                                                int.Parse(make.CertCode) <= 
  3.                                                                                (startcode + count - 1) && 
  4.                                                                                make.ProductName == productName); 
var query = context.CertInfoMakeDetails.ToList().Where(make => int.Parse(make.CertCode) >= startcode &&
                                                                               int.Parse(make.CertCode) <=
                                                                               (startcode + count - 1) &&
                                                                               make.ProductName == productName);

这样做的缺点是:即使我们相应的实体已经被DbContext缓存,EF还是会去执行数据库访问,而数据库访问是被普遍认为比较耗费性能的。

从EF4.1开始,微软为我们提供了一个新的API:DbSet<>.Find()它可以帮助我们通过主键来查找对应的实体。并且如果相应的实体已经被DbContext缓存,EF会在缓存中直接返回对应的实体,而不会执行数据库访问。

比如我们查找证书ID为"101"的实体:

  1. var query = context.CertInfoMakeDetails.Find("101"); 
var query = context.CertInfoMakeDetails.Find("101");

也可以使用联合主键(比如查找CertCode = "101", ProductName = "ABC")的实体:

  1. var query = context.CertInfoMakeDetails.Find("101", "ABC"); 
var query = context.CertInfoMakeDetails.Find("101", "ABC");

注意:此处输入联合主键的次序需要按照我们定义改实体类时声明主键的次序。

和之前使用Where或First调用不同,Find也可以找到刚刚新增的实体:

  1. using (var context = new MyContext()) 
  2.         context.CertInfoMakeDetails.Add(new CertInfoMakeDetail {....}); 
  3.         var newOne = context.Find(“Id”); 
using (var context = new MyContext())
{
        context.CertInfoMakeDetails.Add(new CertInfoMakeDetail {....});
        var newOne = context.Find(“Id”);
}

最后让我们来看看Find是如何实现的:

  1. public TEntity Find(params object[] keyValues) 
  2.     this.InternalContext.ObjectContext.AsyncMonitor.EnsureNotEntered(); 
  3.     this.InternalContext.DetectChanges(false); 
  4.     WrappedEntityKey key = new WrappedEntityKey(this.EntitySet, this.EntitySetName, keyValues, "keyValues"); 
  5.     object obj2 = this.FindInStateManager(key) ?? this.FindInStore(key, "keyValues"); 
  6.     if ((obj2 != null) && !(obj2 is TEntity)) 
  7.     { 
  8.         throw System.Data.Entity.Resources.Error.DbSet_WrongEntityTypeFound(obj2.GetType().Name, typeof(TEntity).Name); 
  9.     } 
  10.     return (TEntity) obj2; 
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");
    if ((obj2 != null) && !(obj2 is TEntity))
    {
        throw System.Data.Entity.Resources.Error.DbSet_WrongEntityTypeFound(obj2.GetType().Name, typeof(TEntity).Name);
    }
    return (TEntity) obj2;
}

首先,EF调用了EnsureNotEntered() 方法来检查内部ObjectContext的状态,如果这个实例当前获得了一个排它锁,那么就抛出异常。之后调用DetectChanges 方法来同步了对象状态。WrappedEntityKey 主要用来将Find函数的参数包装成一个KeyValuePair。

之后,Find 方法首先调用 FindInStateManager 方法在缓存中进行查找,如果找不到对应的实体,那么就调用 FindInStore 在对数据库进行查找,如果仍然找不到,就抛出异常,否则返回对应实体。

原文地址:https://www.cnblogs.com/cuihongyu3503319/p/8671162.html