|
| 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>`__ |
0 commit comments