Skip to content

Commit 0777612

Browse files
DOCSP-32166 - Write Basics (#16)
1 parent f052632 commit 0777612

File tree

4 files changed

+181
-20
lines changed

4 files changed

+181
-20
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ Fundamentals
1313

1414
/fundamentals/configure
1515
/fundamentals/query-data
16+
/fundamentals/write-data
1617

1718
- :ref:`entity-framework-configure`

source/fundamentals/query-data.txt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ the planet name to the console:
5858
:language: csharp
5959
:dedent:
6060

61-
To learn more about the ``FirstOrDefault()`` method, see the `.NET API documentation
62-
<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.firstordefault?view=net-8.0>`__.
63-
6461
Find Multiple Entities
6562
~~~~~~~~~~~~~~~~~~~~~~
6663

@@ -77,9 +74,6 @@ The following code uses the ``Where()`` method to find all planets that have the
7774
:language: csharp
7875
:dedent:
7976

80-
To learn more about the ``Where()`` method, see the `.NET API documentation
81-
<https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.query.queryablemethods.where>`__.
82-
8377
Sort Entities
8478
-------------
8579

@@ -117,17 +111,11 @@ prints the results to the console.
117111
Uranus
118112
Neptune
119113

120-
To learn more about the ``OrderBy()`` method, see the `.NET API documentation
121-
<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderby>`__.
122-
123114
.. tip:: Sort in Descending Order
124115

125116
You can sort the results of a query in descending order by using the
126117
``OrderByDescending()`` method.
127118

128-
To learn more about the ``OrderByDescending()`` method, see the `.NET API documentation
129-
<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderbydescending>`__.
130-
131119
You can perform a secondary sort on your query by using the ``ThenBy()`` method. The
132120
``ThenBy()`` method sorts the results of the ``OrderBy()`` method in ascending
133121
order based on a specified sort criteria. The ``ThenBy()`` method should be
@@ -138,9 +126,6 @@ chained to the ``OrderBy()`` method.
138126
You can perform a secondary sort in descending order by using the
139127
``ThenByDescending()`` method.
140128

141-
To learn more about the ``ThenByDescending()`` method, see the `.NET API documentation
142-
<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenbydescending>`__.
143-
144129
The following code uses the ``OrderBy()`` and ``ThenBy()`` methods to find all
145130
planets and sort them by the ``hasRings()`` field, with a secondary sort
146131
on the ``name`` field.
@@ -176,5 +161,15 @@ on the ``name`` field.
176161
When sorting on fields with a boolean value, entities with a field value of
177162
``false`` show before those with a value of ``true``.
178163

179-
To learn more about the ``ThenBy()`` method, see the `.NET API documentation
180-
<https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenby>`__.
164+
Additional Information
165+
----------------------
166+
167+
To learn more about the methods discussed in this guide, see the following .NET API
168+
documentation links:
169+
170+
- `FirstOrDefault() <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.firstordefault>`__
171+
- `Where() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.query.queryablemethods.where>`__
172+
- `OrderBy() <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderby>`__
173+
- `OrderByDescending() <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.orderbydescending>`__
174+
- `ThenBy() <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenby>`__
175+
- `ThenByDescending() <https://learn.microsoft.com/en-us/dotnet/api/system.linq.queryable.thenbydescending>`__

source/fundamentals/write-data.txt

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
.. _entity-framework-write-data:
2+
3+
=====================
4+
Write Data to MongoDB
5+
=====================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: EF, EF Core, code example, write
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
{+framework-core+} allows you to work with data in your application without
24+
explicitly running database commands. You can insert, update, or delete data
25+
within your application and persist those changes to MongoDB by using the
26+
``SaveChanges()`` method.
27+
28+
When you call the ``SaveChanges()`` method, the {+provider-short+} automatically detects
29+
any changes made to your data and runs the necessary commands to update the
30+
database by using the MongoDB Query API.
31+
32+
In this guide, you can see examples of how to perform common write operations on
33+
an application configured to use the {+provider-long+}.
34+
35+
.. tip::
36+
37+
To learn how to configure an application to use the {+provider-short+}, see
38+
:ref:`entity-framework-configure`.
39+
40+
Insert
41+
------
42+
43+
You can use the ``Add()`` method to insert a single entity into your collection,
44+
or you can use the ``AddRange()`` method to insert multiple entities at once.
45+
46+
Insert One Entity
47+
~~~~~~~~~~~~~~~~~
48+
49+
The ``Add()`` method accepts a single entity of the same type that you
50+
specified on the ``DbSet`` instance that you are modifying.
51+
52+
The following code uses the ``Add()`` method to add a new ``Planet`` object to
53+
the ``DbSet`` called ``Planets``. It then calls the ``SaveChanges()`` method to
54+
insert that entity into the MongoDB collection.
55+
56+
.. literalinclude:: /includes/code-examples/quick-reference.cs
57+
:start-after: // start-insert-one
58+
:end-before: // end-insert-one
59+
:language: csharp
60+
:dedent:
61+
62+
Insert Multiple Entities
63+
~~~~~~~~~~~~~~~~~~~~~~~~
64+
65+
The ``AddRange()`` method accepts an array of entities that you want to add to the ``DbSet``.
66+
67+
The following code uses the ``AddRange()`` method to add an array of ``Planet``
68+
objects to the ``DbSet`` called ``Planets``. It then calls the ``SaveChanges()``
69+
method to insert those entities into the MongoDB collection.
70+
71+
.. literalinclude:: /includes/code-examples/quick-reference.cs
72+
:start-after: // start-insert-many
73+
:end-before: // end-insert-many
74+
:language: csharp
75+
:dedent:
76+
77+
Update
78+
------
79+
80+
To update an entity, first retrieve the entity that you want to update. Then
81+
make the changes to that entity. The provider tracks any changes made to the entity, such as setting
82+
properties or adding and removing items from fields with list values. To save the update to
83+
MongoDB, call the ``SaveChanges()`` method. The {+provider-short+} compares the updated entity with a
84+
snapshot of the entity before the change and automatically updates the collection
85+
by using the MongoDB Query API.
86+
87+
The following code retrieves an entity in which the ``name`` value is
88+
``"Mercury"``, then updates the ``name`` field. The code then calls the ``SaveChanges()``
89+
method to persist that change to the collection.
90+
91+
.. literalinclude:: /includes/code-examples/quick-reference.cs
92+
:start-after: // start-update-one
93+
:end-before: // end-update-one
94+
:language: csharp
95+
:dedent:
96+
97+
Delete
98+
------
99+
100+
You can use the ``Remove()`` method to delete a single entity from your
101+
collection, or the ``RemoveRange()`` method to delete multiple entities at
102+
once.
103+
104+
Delete One Entity
105+
~~~~~~~~~~~~~~~~~
106+
107+
The ``Remove()`` method accepts a single entity of the same type that you
108+
specified on the ``DbSet`` instance that you are modifying.
109+
110+
The following code removes a ``Planet`` entity in which the ``name`` value is
111+
``"Pluto"``. It then calls the ``SaveChanges()`` method to delete that entity from
112+
the MongoDB collection.
113+
114+
.. literalinclude:: /includes/code-examples/quick-reference.cs
115+
:start-after: // start-delete-one
116+
:end-before: // end-delete-one
117+
:language: csharp
118+
:dedent:
119+
120+
Delete Multiple Entities
121+
~~~~~~~~~~~~~~~~~~~~~~~~
122+
123+
The ``RemoveRange()`` method accepts an array of entities to remove from the
124+
``DbSet``.
125+
126+
The following code finds two ``Planet`` entities and adds them to an array. It
127+
then uses the ``RemoveRange()`` method to remove both entities from the
128+
``DbSet``. Finally, it uses the ``SaveChanges()`` method to remove those
129+
entities from the MongoDB collection.
130+
131+
.. literalinclude:: /includes/code-examples/quick-reference.cs
132+
:start-after: // start-delete-many
133+
:end-before: // end-delete-many
134+
:language: csharp
135+
:dedent:
136+
137+
Additional Information
138+
----------------------
139+
140+
To learn more about the methods discussed in this guide, see the following {+language-dotnet+} API
141+
documentation links:
142+
143+
- `SaveChanges() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.saveChanges>`__
144+
- `Add() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.add>`__
145+
- `AddRange() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.addRange>`__
146+
- `Remove() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.remove>`__
147+
- `RemoveRange() <https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.removeRange>`__

source/includes/code-examples/quick-reference.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ void FindMultiple()
3232
void InsertOne()
3333
{
3434
// start-insert-one
35-
db.Planets.Add(new Planet() { name = "Pluto", hasRings = false, orderFromSun = 9 });
35+
db.Planets.Add(new Planet()
36+
{
37+
name = "Pluto",
38+
hasRings = false,
39+
orderFromSun = 9
40+
});
41+
3642
db.SaveChanges();
3743
// end-insert-one
3844
}
@@ -42,8 +48,20 @@ void InsertMany()
4248
// start-insert-many
4349
var planets = new[]
4450
{
45-
new Planet() { _id = ObjectId.GenerateNewId(), name = "Pluto", hasRings = false, orderFromSun = 9 },
46-
new Planet() { _id = ObjectId.GenerateNewId(), name = "Scadrial", hasRings = false, orderFromSun = 10 }
51+
new Planet()
52+
{
53+
_id = ObjectId.GenerateNewId(),
54+
name = "Pluto",
55+
hasRings = false,
56+
orderFromSun = 9
57+
},
58+
new Planet()
59+
{
60+
_id = ObjectId.GenerateNewId(),
61+
name = "Scadrial",
62+
hasRings = false,
63+
orderFromSun = 10
64+
}
4765
};
4866

4967
db.Planets.AddRange(planets);

0 commit comments

Comments
 (0)