Skip to content

Initializing DbContext of 500+ tables database takes over 3 minutes every application restart #10053

Closed as duplicate
@ngoov

Description

@ngoov

It takes a while (3 minutes+) to 'create/compile' my DbContext. The web server starts in about 5 seconds, but when I do the first query to my database, EF 2.0 has to 'build/create/compile' the database in memory or something, I think? The next requests are almost instant. This was a Database First creation of the DbContext, the database already exists in MSSQL and has data. The DbContext contains about 500 DbSet's with relations.

Is there a way to speed this up by doing the 'creation' of my Entity Framework's DbContext before running the web server (at developmenttime), create the file/mappings it needs, so the first request is fast too?

The web application itself starts in about 5 seconds, which is acceptable. It's the first query through EF Core 2.0 that takes 3 minutes+. If I just show a HTML page, it is loaded after about 5 seconds. The same if I use ADO.NET, the query is shown after 2 seconds. It's the first query (even a simple db.Articles.FirstOrDefault()) that takes 3 minutes+, the subsequent query (any query) takes a normal 100ms or something.

I've tried this in debug as well as release.

Because probably EF is bootstrapping itself, which I'm asking if the result of this bootstrap can be cached, so that EF does not need to rebuild the whole bootstrap every run and instead read the cached version.

Further technical details

EF Core version: 2.0.0 (was same result on 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: tested on Windows 8, Windows 10, Windows Server 2016, Windows Server 2012 R2, same result on all
IDE: Visual Studio 15.3.5 (2017) Pro

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions