简体   繁体   English

实体框架缓慢的第一次调用IRepository DbContext

[英]Entity Framework Slow First Call IRepository DbContext

We are using entity framework 6.1.1 with a DbContext like below and EntityTypeConfiguration to map approximately 400 entities to our DbContext. 我们将实体框架6.1.1与下面的DbContext和EntityTypeConfiguration一起使用,以将大约400个实体映射到我们的DbContext。 We then create an instance of our DbContext and use it to create the object sets for each IRepository entity we use in our service layer. 然后,我们创建DbContext的实例,并使用它为我们在服务层中使用的每个IRepository实体创建对象集。 The problem we are having which I cannot find a solution to is that the first call to the db is taking approximately 18 seconds when we are using Ants profiler. 我遇到的问题是我找不到解决方案,这是在使用Ants Profiler时,第一次调用数据库大约需要18秒。

I have looked into generating the views but I cannot find a way to do that when the DbContext does not contain hard-coded DbSet collections to the entities. 我已经研究过生成视图,但是当DbContext不包含对实体的硬编码DbSet集合时,我找不到找到该视图的方法。 Is there a way to pre-generate the views with our pattern and if so will we see a significant performance improvement? 有没有一种方法可以使用我们的模式预先生成视图,如果可以的话,我们是否会看到性能上的显着改善?

Or is it time to go down a different path, should we create smaller DbContexts which are for specific areas of the database on logical separations? 还是该走上一条不同的道路了,我们是否应该创建较小的DbContext,这些DbContext用于逻辑分离的数据库特定区域?

  public class Context: DbContext
  {
#pragma warning disable
    Type dummyType_SqlProviderServices = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
#pragma


static Context()
{
  Database.SetInitializer(new ContextatabaseInitializer<Context>());

}

public Context(DbConnection con)
  : base(con, false)
{
}

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Configurations.AddFromAssembly(typeof(ZincContext).Assembly);      
    base.OnModelCreating(modelBuilder);
  }
}

public class EntityRepository<T> : IEntityRepository<T> where T : class
{
    protected IDbSet<T> ObjectSet
    {
      get
      {
        if (_objectSet == null)
        {
          _objectSet = this.DbContext.Set<T>();
        }
        return _objectSet;
      }
    }
}

Generating views based on an entity model is a significant cost the first time that an application executes a query. 基于实体模型生成视图是应用程序首次执行查询时的重大成本。 Use the EdmGen.exe utility to pre-generate views as a Visual Basic or C# code file that can be added to the project during design. 使用EdmGen.exe实用工具将视图预先生成为Visual Basic或C#代码文件,可以在设计过程中将其添加到项目中。 You could also use the Text Template Transformation Toolkit to generate pre-compiled views. 您还可以使用“文本模板转换工具包”来生成预编译的视图。 Pre-generated views are validated at runtime to ensure that they are consistent with the current version of the specified entity model. 预生成的视图在运行时进行验证,以确保它们与指定实体模型的当前版本一致。 For more information, see How to: 有关更多信息,请参见如何:

Pre-Generate Views to Improve Query Performance (Entity Framework) 预先生成视图以提高查询性能(实体框架)

Isolating Performance with Precompiled/Pre-generated Views in the Entity Framework 4 使用Entity Framework 4中的预编译/预生成的视图隔离性能

ENTITY FRAMEWORK CODE FIRST VIEW GENERATION TEMPLATES ON VISUAL STUDIO CODE GALLERY . 可视化STUDIO代码库中的实体框架代码第一视图生成模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM