请帮我看看是什么让我的功能变慢

时间:2016-05-27 20:08:56

标签: asp.net-mvc stored-procedures entity-framework-6 repository-pattern unit-of-work

使用工作单元存储库模式

private UnitOfWork unitOfWork = new UnitOfWork();

private Entities _Entities = new Entities();

var filing_xml = unitOfWork.T_FILING_XMLRepository.Get().Where(a =>
    a.filing_id == filingID).FirstOrDefault();

这需要大约10秒来获取数据

var filing_xml = _Entities.T_FILING_XML.Where(a => a.filing_id == filingID).FirstOrDefault();

这需要大约2秒来获取数据

是否有任何解决方案可以让工作单位更快?

public virtual IEnumerable<TEntity> Get(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
{
    IQueryable<TEntity> query = dbSet;

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

    foreach (var includeProperty in includeProperties.Split
        (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
        query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
        return orderBy(query).ToList();
    }
    else
    {
        return query.ToList();
    }
}

工作单位

public GenericRepository<T_FILING_XML> T_FILING_XMLRepository
{
    get
    {
        if (this.t_filing_xmlRepository == null)
        {
            this.t_filing_xmlRepository = new GenericRepository<T_FILING_XML>(context);
        }
        return t_filing_xmlRepository;
    }
}

1 个答案:

答案 0 :(得分:2)

代码#1

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("LabelRectangleTest.fxml"));
            HBox root = loader.load();
            Scene scene = new Scene(root,400,400);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

您正在从数据库中获取所有记录,然后在应用程序中过滤结果以仅获取一条记录。你的Get方法有一个filter参数,为什么不使用它呢?

使用下面的代码,您只能从数据库中获取一条记录。

private Entities _Entities = new Entities();

var filing_xml = unitOfWork.T_FILING_XMLRepository.Get().Where(a =>
    a.filing_id == filingID).FirstOrDefault();