• What difference does .AsNoTracking() make?


    What difference does .AsNoTracking() make?

    问题

    I have a question regarding the .AsNoTracking() extension, as this is all quite new and quite confusing.

    I'm using a per-request context for a website.

    A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.

    This example is what I'm currently doing:

    context.Set<User>().AsNoTracking()
    // Step 1) Get user
    context.Set<User>()
    // Step 2) Update user

    This is the same as above but removing the .AsNoTracking() from Step 1:

    context.Set<User>();
    // Step 1) Get user
    context.Set<User>()
    // Step 2) Update user

    The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.

    Can anyone tell me what the difference is?

    回答1

    The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.

    回答2

    see this page Entity Framework and AsNoTracking

    What AsNoTracking Does

    Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking(). This optimisation allows you to tell Entity Framework not to track the results of a query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.

    there are significant performance gains to be had by using AsNoTracking

    回答3

    No Tracking LINQ to Entities queries

    Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance

    Pros

    1. Improved performance over regular LINQ queries.
    2. Fully materialized objects.
    3. Simplest to write with syntax built into the programming language.

    Cons

    1. Not suitable for CUD operations.
    2. Certain technical restrictions, such as: Patterns using DefaultIfEmpty for OUTER JOIN queries result in more complex queries than simple OUTER JOIN statements in Entity SQL.
    3. You still can’t use LIKE with general pattern matching.

    More info available here:

    Performance considerations for Entity Framework

    Entity Framework and NoTracking

    Tracking vs. No-Tracking Queries

    No-tracking queries

    No tracking queries are useful when the results are used in a read-only scenario. They're quicker to execute because there's no need to set up the change tracking information. If you don't need to update the entities retrieved from the database, then a no-tracking query should be used. You can swap an individual query to be no-tracking. No tracking query will also give you results based on what is in the database disregarding any local changes or added entities.

  • 相关阅读:
    Mac 中 go env 找不到 GOROOT
    git log 中想要更改历史 日志
    Go 中 生成随机数的坑 rand.seed() rand.intn()
    HTML 首页倒三角形导航块
    HTML overflow:hidden
    HTML padding ,margin
    JavaScript 通过关系找标签
    JavaScript Dom编程根据属性找节点
    javascript String
    HTML 标签包含规范,规避脱标流,图片和文字垂直居中对齐,
  • 原文地址:https://www.cnblogs.com/chucklu/p/16624208.html
Copyright © 2020-2023  润新知