locked
Is cached db model store available for Entityframework Core RRS feed

  • Question

  • User283528319 posted

    Hi all,

    It is really weird that EF has a problem with "First time query execution" due to model creation. (it takes 10-20 seconds !!!)

    as far as I see it is solved in EF 6 using the methods below. ( https://codeopinion.com/entity-framework-code-first-model-cache/ )

    however, I couldn't find an equivalant version for EF core.

    Could someone help me please.

    Tuesday, July 23, 2019 3:07 AM

Answers

  • User1520731567 posted

    Hi fatihbarut,

    Yes.

    Like the below code:

     services.AddDbContext<ApplicationDbContext>(options =>
         options.UseSqlServer(
             Configuration.GetConnectionString("DefaultConnection")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));

    You could refer to more details,like:

    https://rehansaeed.com/optimally-configuring-entity-framework-core/

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2019 5:27 AM

All replies

  • User-821857111 posted

    It's not relevant to EF Core, because the EDMX approach is not supported at all.

    Tuesday, July 23, 2019 6:58 AM
  • User283528319 posted

    any other solution to save the day?

    beside "loading..." screen that I use now?

    Tuesday, July 23, 2019 10:40 AM
  • User753101303 posted

    Hi,

    According to https://github.com/aspnet/EntityFrameworkCore/issues/1906 apparently not.

    Tuesday, July 23, 2019 11:35 AM
  • User1520731567 posted

    Hi fatihbarut,

    You could refer to this link.

    The EF Core by default tracks all the entities you run queries against.

    When you run it for the first time the track change mechanism kicks in... that's why it takes a little bit longer.

    You can avoid this, especially when retrieving collections by using .AsNoTracking() when composing the query.

    var items = DbContext.MyDbSet
        .AsNoTracking()
    .Include(_=>_.xxx) .ToList();

    In addition,EF core needs to compile LINQ quires using reflection therefor first queries are always slow.

    There is already a GitHub issue here.

    Best Regards.

    Yuki Tao
     

    Wednesday, July 24, 2019 2:30 AM
  • User283528319 posted

    Hi fatihbarut,

    You could refer to this link.

    The EF Core by default tracks all the entities you run queries against.

    When you run it for the first time the track change mechanism kicks in... that's why it takes a little bit longer.

    You can avoid this, especially when retrieving collections by using .AsNoTracking() when composing the query.

    var items = DbContext.MyDbSet
        .AsNoTracking()
        .Include(_=>_.xxx)
        .ToList();

    In addition,EF core needs to compile LINQ quires using reflection therefor first queries are always slow.

    There is already a GitHub issue here.

    Best Regards.

    Yuki Tao
     

    Is there any global setting for it under startup.cs?

    Wednesday, July 24, 2019 7:57 AM
  • User1520731567 posted

    Hi fatihbarut,

    Yuki Tao

    Hi fatihbarut,

    You could refer to this link.

    The EF Core by default tracks all the entities you run queries against.

    When you run it for the first time the track change mechanism kicks in... that's why it takes a little bit longer.

    You can avoid this, especially when retrieving collections by using .AsNoTracking() when composing the query.

    var items = DbContext.MyDbSet
        .AsNoTracking()
        .Include(_=>_.xxx)
        .ToList();

    In addition,EF core needs to compile LINQ quires using reflection therefor first queries are always slow.

    There is already a GitHub issue here.

    Best Regards.

    Yuki Tao
     

    Is there any global setting for it under startup.cs?

    Which one are you talking about?

    Thursday, July 25, 2019 6:31 AM
  • User283528319 posted
    AsNoTracking()
    Thursday, July 25, 2019 8:34 AM
  • User1520731567 posted

    Hi fatihbarut,

    Yes.

    Like the below code:

     services.AddDbContext<ApplicationDbContext>(options =>
         options.UseSqlServer(
             Configuration.GetConnectionString("DefaultConnection")).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));

    You could refer to more details,like:

    https://rehansaeed.com/optimally-configuring-entity-framework-core/

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2019 5:27 AM
  • User283528319 posted

    great thanks

    Tuesday, July 30, 2019 6:54 AM