Skip to content

Commit 31d0dc1

Browse files
DOCSP-41879 Clarify SaveChanges is transactional (#34)
1 parent 4a08f4e commit 31d0dc1

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

source/fundamentals/write-data.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Overview
2323
{+framework-core+} allows you to work with data in your application without
2424
explicitly running database commands. You can insert, update, or delete data
2525
within your application and persist those changes to MongoDB by using the
26-
``SaveChanges()`` method.
26+
``SaveChanges()`` or ``SaveChangesAsync()`` method.
2727

28-
When you call the ``SaveChanges()`` method, the {+provider-short+} automatically detects
28+
When you call the ``SaveChanges()`` or ``SaveChangesAsync()`` method, the {+provider-short+} automatically detects
2929
any changes made to your data and runs the necessary commands to update the
3030
database by using the MongoDB Query API.
3131

@@ -37,13 +37,40 @@ an application configured to use the {+provider-short+}.
3737
To learn how to configure an application to use the {+provider-short+}, see
3838
:ref:`entity-framework-configure`.
3939

40+
Transactional Write Operations
41+
-------------------------------
42+
43+
The ``SaveChanges()`` and ``SaveChangesAsync()`` methods are transactional by
44+
default. This means that if an error occurs during an operation, the provider
45+
rolls back any changes made to the database. Because of this, your application
46+
must be connected to a transaction-capable deployment of MongoDB server, such as
47+
a replica set.
48+
49+
You can disable automatic transactions in the ``SaveChanges()`` and
50+
``SaveChangesAsync()`` methods by setting the ``AutoTransactionBehavior`` property to
51+
``AutoTransaction.Never`` on your ``DbContext`` subclass during application
52+
setup. However, we do not recommend disabling this feature. Doing so causes
53+
any concurrency changes or operation failures during the save operation
54+
to leave the database in an inconsistent state.
55+
56+
The following example shows how to disable automatic transactions in the
57+
``SaveChanges()`` and ``SaveChangesAsync()`` methods:
58+
59+
.. code-block:: csharp
60+
61+
dbContext.Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;
62+
63+
.. warning::
64+
65+
Disabling automatic transactions can lead to data inconsistencies. We
66+
recommend that you do not disable this feature.
67+
4068
Insert
4169
------
4270

4371
You can use the ``Add()`` method to insert a single entity into your collection,
4472
or you can use the ``AddRange()`` method to insert multiple entities at once.
4573

46-
4774
.. _entity-framework-insert-one:
4875

4976
Insert One Entity

source/includes/fundamentals/code-examples/configure/ConfigureEFProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
new DbContextOptionsBuilder<MyDbContext>().UseMongoDB(mongoClient, "<Database Name>");
1111

1212
var db = new MyDbContext(dbContextOptions.Options);
13+
db.Database.EnsureCreated();
1314

1415
// Add a new customer and save it to the database
1516
db.Customers.Add(new Customer() { name = "John Doe", Order = "1 Green Tea" });

source/includes/fundamentals/code-examples/configure/UseMongoDB.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
var dbContextOptions =
44
new DbContextOptionsBuilder<MyDbContext>().UseMongoDB(mongoClient, "<Database Name");
55

6-
var db = new MyDbContext(dbContextOptions.Options);
6+
var db = new MyDbContext(dbContextOptions.Options);
7+
db.Database.EnsureCreated();

source/limitations.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,6 @@ supports only the following scalar aggregation operations:
6464
This version of the {+provider-short+} does not support other scalar aggregation
6565
operations.
6666

67-
Transactions
68-
~~~~~~~~~~~~
69-
70-
Transactions ensure that all updates in a set of changes either succeed, or fail
71-
together so the database is not left in an inconsistent state.
72-
73-
This version of the {+provider-short+} does not support the {+framework-core+}
74-
transaction model.
75-
7667
Migrations
7768
~~~~~~~~~~
7869

0 commit comments

Comments
 (0)