Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用不同DBContext进行缓存_C#_Entity Framework_Caching - Fatal编程技术网

C# 使用不同DBContext进行缓存

C# 使用不同DBContext进行缓存,c#,entity-framework,caching,C#,Entity Framework,Caching,我正在使用通用存储库的UnitOfWork模式。每当查询数据库时,我都实现了内存缓存。装载记录完全正确。我遇到的问题是,如果我要创建一个新记录来实例化一个不同于案例中使用的任何属性的新UnitOfWOrk 比如说 MemoryCache存储了一个种族列表 伪码 我创建了一个新的Person并设置Person.ocidentity=MemoryCache.Ethnicities.First(x=>x.type==type) 当我试图保存时,它向我提供了以下错误一个实体对象不能被多个IEntityC

我正在使用通用存储库的UnitOfWork模式。每当查询数据库时,我都实现了内存缓存。装载记录完全正确。我遇到的问题是,如果我要创建一个新记录来实例化一个不同于案例中使用的任何属性的新UnitOfWOrk

比如说 MemoryCache存储了一个种族列表

伪码

我创建了一个新的Person并设置Person.ocidentity=MemoryCache.Ethnicities.First(x=>x.type==type)

当我试图保存时,它向我提供了以下错误
一个实体对象不能被多个IEntityChangeTracker实例引用
。如果我不实例化一个新的UnitOfWork,我可以去掉它

所以现在我有点为难了,我使用单例Dbcontext吗?随着时间的推移,它可能会变得非常沉重和缓慢,或者我只是完全摆脱了缓存。我的系统目前所做的是在需要任何东西的时候查询数据库。我正试图减少对数据库的查询数量,因为客户端的互联网连接很糟糕

public override IEnumerable<T> Get(Expression<Func<T, bool>> filter = null,
    Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
    int? numberOfEntities = null,
    IEnumerable<string> includeProperties = null)
{
    IQueryable<T> query = DbSet;
    List<T> result = new List<T>();

    if (filter != null)
    {
        query = query.Where(filter);
    }

    if (includeProperties != null)
    {
        query = includeProperties.Aggregate(query,
            (current, includeProperty) => current.Include(includeProperty));
    }

    if (orderBy != null)
    {
        query = orderBy(query);
    }

    var actualNumberOfEntities = Int32.MaxValue;

    if (numberOfEntities.HasValue)
    {
        actualNumberOfEntities = Math.Max(1, numberOfEntities.Value);
    }

    if (MemoryCache.Default[query.ToString()] == null)
    {        
        result = query.AsExpandable().Take(actualNumberOfEntities).ToList();
        MemoryCache.Default[query.ToString()] = result;
        return result;
    }
    else
    {
        result = (MemoryCache.Default[query.ToString()] as IEnumerable<T>).ToList();
    }

    return result;

}
public重写IEnumerable Get(表达式过滤器=null,
Func orderBy=null,
int?numberOfEntities=null,
IEnumerable includeProperties=null)
{
IQueryable query=DbSet;
列表结果=新列表();
if(过滤器!=null)
{
query=query.Where(过滤器);
}
if(includeProperties!=null)
{
query=includeProperties.Aggregate(查询,
(当前,includeProperty)=>current.Include(includeProperty));
}
if(orderBy!=null)
{
query=orderBy(查询);
}
var ActualNumber of Entities=Int32.MaxValue;
if(numberOfEntities.HasValue)
{
实际实体数=Math.Max(1,numberOfEntities.Value);
}
if(MemoryCache.Default[query.ToString()]==null)
{        
结果=query.AsExpandable().Take(实际实体数).ToList();
MemoryCache.Default[query.ToString()]=result;
返回结果;
}
其他的
{
结果=(MemoryCache.Default[query.ToString()]为IEnumerable.ToList();
}
返回结果;
}

您必须小心,因为您打破了一个重要的EF设计概念。因为您是通过从缓存中而不是从数据库中查找来检索数据的。只需使用第二级缓存,您还可以控制dbContext实时,将其与UnitOfWork结合起来。使unitOfWork成为一次性的,并在使用范围内使用它。否则,DbContext一级缓存将缓存大量实体,并且随着时间的推移会变得缓慢,在糟糕的使用情况下,您会遇到奇怪的错误。@BassamAlugili感谢您为我指出了正确的方向=)您必须小心,您正在打破一个重要的EF设计概念。因为您是通过从缓存中而不是从数据库中查找来检索数据的。只需使用第二级缓存,您还可以控制dbContext实时,将其与UnitOfWork结合起来。使unitOfWork成为一次性的,并在使用范围内使用它。否则,DbContext一级缓存将缓存大量实体,并且随着时间的推移会变得缓慢,在糟糕的使用情况下,您会遇到奇怪的错误。@BassamAlugili感谢您为我指明了正确的方向=)