|
| 1 | +.. _entity-framework-configure: |
| 2 | + |
| 3 | +=========================================== |
| 4 | +Configure Entity Framework Core for MongoDB |
| 5 | +=========================================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: EF, EF Core, code example |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +In this guide, you will learn how to configure an application to use the |
| 21 | +{+provider-long+}. To learn how to set up a new project and install the |
| 22 | +{+provider-short+}, see the :ref:`entity-framework-quickstart`. |
| 23 | + |
| 24 | +Create a POCO |
| 25 | +------------- |
| 26 | + |
| 27 | +Create a `Plain old CLR/Class object |
| 28 | +<https://en.wikipedia.org/wiki/Plain_old_CLR_object>`__, or **POCO**, to use as |
| 29 | +a model for your entity. A POCO is a simple class object that doesn't inherit |
| 30 | +features from any framework-specific base classes or interfaces. |
| 31 | + |
| 32 | +The following code example shows how to create a POCO that represents a customer: |
| 33 | + |
| 34 | +.. literalinclude:: /includes/fundamentals/code-examples/configure/Customer.cs |
| 35 | + :language: csharp |
| 36 | + :copyable: |
| 37 | + |
| 38 | +.. tip:: |
| 39 | + |
| 40 | + To learn more about POCOs, see the :driver:`POCO guide |
| 41 | + </csharp/current/fundamentals/serialization/poco/>` in the |
| 42 | + {+csharp-driver-short+} documentation. |
| 43 | + |
| 44 | +Create a DB Context Class |
| 45 | +------------------------- |
| 46 | + |
| 47 | +To begin using {+framework-core+}, create a context class that derives from |
| 48 | +`DBContext <https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext>`__. |
| 49 | +The ``DbContext`` derived class instance represents a database session and is used to |
| 50 | +query and save instances of your entities. |
| 51 | + |
| 52 | +The ``DBContext`` class exposes ``DBSet`` properties that specify the entities you |
| 53 | +can interact with while using that context. |
| 54 | + |
| 55 | +The following example creates an instance of a ``DBContext`` derived class and |
| 56 | +specifies the ``Customer`` object as a ``DBSet`` property: |
| 57 | + |
| 58 | +.. literalinclude:: /includes/fundamentals/code-examples/configure/MyDBContext.cs |
| 59 | + :language: csharp |
| 60 | + :copyable: |
| 61 | + |
| 62 | +The previous code example overrides the ``OnModelCreating()`` method. Overriding |
| 63 | +the ``OnModelCreating()`` method allows you to specify configuration details for your |
| 64 | +model and its properties. This example uses the ``ToCollection()`` method to |
| 65 | +specify that the ``Customer`` entities in your application map to the |
| 66 | +``customers`` collection in MongoDB. |
| 67 | + |
| 68 | +.. TODO: Link to a page that goes over methods available to use in OnModelCreating |
| 69 | + |
| 70 | +Use MongoDB |
| 71 | +----------- |
| 72 | + |
| 73 | +Once you've created a ``DBContext`` class, construct a |
| 74 | +``DbContextOptionsBuilder`` object and call its ``UseMongoDB()`` method. This |
| 75 | +method takes two parameters: a ``MongoClient`` instance and |
| 76 | +the name of the database that stores the collections you are working with. |
| 77 | + |
| 78 | +The ``UseMongoDB()`` method returns a ``DbContextOptions`` object. Pass the |
| 79 | +``Options`` property of this object to the constructor for your ``DBContext`` |
| 80 | +class. |
| 81 | + |
| 82 | +The following example shows how to construct a ``DBContext`` object in |
| 83 | +this way: |
| 84 | + |
| 85 | +.. literalinclude:: /includes/fundamentals/code-examples/configure/UseMongoDB.cs |
| 86 | + :language: csharp |
| 87 | + :copyable: |
| 88 | + |
| 89 | +.. tip:: Creating a MongoClient |
| 90 | + |
| 91 | + You can call methods from the {+csharp-driver-long+} when using |
| 92 | + the {+provider-short+}. The previous example uses the |
| 93 | + ``MongoClient()`` method from the {+csharp-driver-short+} to create a MongoDB |
| 94 | + client that connects to a MongoDB instance. |
| 95 | + |
| 96 | + To learn more about using the {+csharp-driver-long+} |
| 97 | + to connect to MongoDB, see the |
| 98 | + :driver:`Connection guide </csharp/current/fundamentals/connection/connect/>` |
| 99 | + in the {+csharp-driver-short+} documentation. |
| 100 | + |
| 101 | +Example |
| 102 | +------- |
| 103 | + |
| 104 | +The following code example shows how to configure the |
| 105 | +{+provider-short+} and insert a document into the database: |
| 106 | + |
| 107 | +.. literalinclude:: /includes/fundamentals/code-examples/configure/ConfigureEFProvider.cs |
| 108 | + :language: csharp |
| 109 | + :copyable: |
0 commit comments