When using EF core to update MongoDB documents which command is used “Update” or “Replace”? I have a few performance concerns but I cannot seem to find the right answer in the documentation.
Is there a way to print the DB operation using EF for MongoDb?
Hi, @Nicholas_Vella,
Welcome to the MongoDB Community Forums. Our EF Provider does partial updates using the update
command rather than full-document replacement. The 8.1 release added support for optimistic concurrency.
When configuring your DbContext
, you can enable logging as follows:
var options = new DbContextOptionsBuilder<MoviesDbContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
.LogTo(Console.WriteLine)
.EnableSensitiveDataLogging()
.Options;
With logging enabled, the MQL for queries will be logged. It will also display the number of inserts, deletes, and updates made while saving changes. It does not yet render the MQL for data modifications. It should be possible, we just haven’t implemented that feature yet. Please add a feature request if this would be useful to you.
Sincerely,
James
Hi @James_Kovacs ,
Thank you so much for your answer. In the reply it was mentioned that optimistic concurrency was added in 8.1, am I correct to assume this was an answer to my concern for performance?
Are there any plans to add Replace too as a command?
Rendering the MQL for data modifications would be extremely useful, I did open a ticket for this thanks!
Hi Nicholas.
The EF provider uses an UpdateOneModel with $set for each property that has a changed value. This is performed inside a BulkWriteOperation with other changes for that collection all wrapped up inside a transaction.
This granular property-level change-tracking is one of the main benefits of the EF provider over the MongoDB C# Driver.
If you’re looking for Replace semantics you should consider using the C# Driver directly. Between the LINQ provider it has and the full-document replacement options it’s much better suited to the approach you’re looking for.
1 Like
Thank you very much @Damien_Guard !