Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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
C# 在通用存储库模式中使用DBContext显式加载_C#_Entity Framework_Generics_Dbcontext - Fatal编程技术网

C# 在通用存储库模式中使用DBContext显式加载

C# 在通用存储库模式中使用DBContext显式加载,c#,entity-framework,generics,dbcontext,C#,Entity Framework,Generics,Dbcontext,我们的context.Configuration.LazyLoadingEnabled=false; 这是我的参考: 我们的API中有一个方法可以获取一个实体及其所有子实体: [IsQuery] [HttpGet] public async Task<IEnumerable<Blog>> GetAllBlogs() { return this.EntityProvider.Get(this.CachedReader, BlogCore.AllIncludes);

我们的context.Configuration.LazyLoadingEnabled=false; 这是我的参考:

我们的API中有一个方法可以获取一个实体及其所有子实体:

[IsQuery]
[HttpGet]
public async Task<IEnumerable<Blog>> GetAllBlogs()
{
    return this.EntityProvider.Get(this.CachedReader, BlogCore.AllIncludes);
}
[NotNull]
public IQueryable<TEntity> Get(ICacheableReader reader, params Expression<Func<TEntity, object>>[] includes)
{
    return reader.Get<TEntity>(opt => opt.Include(includes));
}



/// <summary>Construct a query which retrieves all entities from the repository.</summary>
        /// <typeparam name="TEntity">The entity type.</typeparam>
        /// <param name="options">The entity query configuration.</param>
        /// <returns>Returns a deferred query.</returns>
        /// <exception cref="ObjectDisposedException">This reader has been disposed.</exception>
        public IQueryable<TEntity> Get<TEntity>(Action<IQueryConfig<TEntity>> options = null) where TEntity : class, IEntity
        {
            this.AssertAccessible();
            return this.ApplyOptions(this.Context.Set<TEntity>(), options);
        }

类似于第一级属性:

public IQueryable<TEntity> Get<TEntity>(
    Action<IQueryConfig<TEntity>> options = null, 
    IEnumerable<Expression<Func<TEntity, Object>>> includeProperties = null) where TEntity : class, IEntity
{
    this.AssertAccessible();
    IQueryable<TEntity> query = this.Context.Set<TEntity>();
    if ( includeProperties != null )
        query = includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

    return this.ApplyOptions(query, options);
}

谢谢,但你的代码是有用的。包括我不想使用。包括,因为当您在第二级中使用大量属性和某些属性时,例如.includex=>x.property.propertychild,它将更改为子查询,我们将遇到性能问题。我想加入子属性而不是子查询。我不知道。Entryquery.Reference是否有帮助?您的性能问题是由于查询还是物化造成的?我不相信这个条目会有帮助,因为它与实体相关,而与集合无关。顺便说一句,加载会命中数据库。通过使用它,您将处于一个导致在本地内存中加载所有数据库的路径中。这些包甚至无法工作。include like i=>i.Community.Name应抛出指定的包含路径无效。EntityType“xxx”未声明名为“name”的导航属性。除非名字是一个实体,但我不这么认为。那么你是如何使用这些代码的呢?不,它现在正在工作,我们有它们的映射。你也可以看看这个
[NotNull]
public IQueryable<TEntity> Get(ICacheableReader reader, params Expression<Func<TEntity, object>>[] includes)
{
    return reader.Get<TEntity>(opt => opt.Include(includes));
}



/// <summary>Construct a query which retrieves all entities from the repository.</summary>
        /// <typeparam name="TEntity">The entity type.</typeparam>
        /// <param name="options">The entity query configuration.</param>
        /// <returns>Returns a deferred query.</returns>
        /// <exception cref="ObjectDisposedException">This reader has been disposed.</exception>
        public IQueryable<TEntity> Get<TEntity>(Action<IQueryConfig<TEntity>> options = null) where TEntity : class, IEntity
        {
            this.AssertAccessible();
            return this.ApplyOptions(this.Context.Set<TEntity>(), options);
        }
public IQueryable<TEntity> Get<TEntity>(
    Action<IQueryConfig<TEntity>> options = null, 
    IEnumerable<Expression<Func<TEntity, Object>>> includeProperties = null) where TEntity : class, IEntity
{
    this.AssertAccessible();
    IQueryable<TEntity> query = this.Context.Set<TEntity>();
    if ( includeProperties != null )
        query = includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

    return this.ApplyOptions(query, options);
}