ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • 10.1.0, 11.0.0, 9.2.0, 8.5.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Dotnet Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Problem

      A bulk ExecuteUpdate setter value lambda that calls Guid.NewGuid, Random.Next or EF.Functions.Random is evaluated once per execution, and the same result is written to every matched document, although the lambda reads as a per-document value:

      db.Orders.ExecuteUpdate(s => s.SetProperty(o => o.Status, o => Guid.NewGuid().ToString()));
      

      Relational providers evaluate such a function per row, so the same code there writes a different value to each row.

      Fix

      Refuse it. BuildSetter throws InvalidOperationException (EF's canonical translation failure; on EF11 the detail is the inner exception) before anything is written, when a value lambda contains such a call. The message says to compute the value first and pass it in as a variable.

      Not checked, deliberately:

      • A value that is not a lambda, e.g. SetProperty(o => o.Status, Guid.NewGuid().ToString()). On EF10+ the C# compiler evaluates it inside the caller's delegate, so the provider only sees the result; EF9 behaves the same for consistency.
      • DateTime.Now and DateTime.UtcNow, which are re-read on every execution.

      Known gap on main: Random.NextDouble, NextSingle, NextInt64 and Guid.CreateVersion7 are turned into query parameters by EF's evaluatable-expression filter before the provider sees them, so they cannot be detected. The native branch keeps them in the tree with MongoEvaluatableExpressionFilter (EF-464).

      This is a behavior break (code that ran now throws) and has a BREAKING-CHANGES.md entry.

      Background

      Found by EF-361 (bulk remediation) and tracked as ajcvickers/mongo-efcore-provider#70; first fixed on the fork's release branch in ajcvickers/mongo-efcore-provider#108. The upstream fix is stacked on the EF-361 PR, mongodb/mongo-efcore-provider#408.

            Assignee:
            Arthur Vickers
            Reporter:
            Arthur Vickers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: