Skip to content

Commit 944ffc4

Browse files
DOCSP-43820 Add indexes FAQ entry (#48)
1 parent 548b9bd commit 944ffc4

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

source/faq.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,30 @@ change tracking, entity-based LINQ operations, and modeling familiar to
4848
- Intelligent object tracking
4949
- Entity-based LINQ operations
5050
- Entity Framework modeling and mapping with the fluent API
51-
- Automatic database updates through change tracking
51+
- Automatic database updates through change tracking
52+
53+
How Can I Manage Indexes with the {+provider-short+}?
54+
-----------------------------------------------------
55+
56+
You can create indexes with the {+provider-short+} by calling the ``HasIndex()``
57+
method in the ``OnModelCreating()`` method of your ``DbContext`` class. To learn
58+
more about how to create indexes with the {+provider-short+}, see the
59+
:ref:`Indexes <entity-framework-indexes>` guide.
60+
61+
Because the {+provider-short+} is built on top of the {+csharp-driver-short+},
62+
you can also manage indexes in your application by using the {+csharp-driver-short+}
63+
directly. To use driver methods in your {+provider-short+}
64+
application, call them on the ``MongoClient`` used to set up your
65+
``DbContext``.
66+
67+
The following example creates indexes on the ``movies`` collection by using
68+
{+csharp-driver-short+} methods:
69+
70+
.. literalinclude:: /includes/code-examples/faq.cs
71+
:language: csharp
72+
:start-after: start-create-index
73+
:end-before: end-create-index
74+
75+
To learn more about creating indexes by using the driver, see the
76+
`Indexes guide <{+driver-docs-root+}/fundamentals/indexes/>`__ in the
77+
{+csharp-driver-long+} documentation.

source/includes/code-examples/faq.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// start-create-index
2+
using MongoDB.Driver;
3+
4+
var client = new MongoClient("<connection string>");
5+
var database = client.GetDatabase("sample_mflix");
6+
await CreateIndexesAsync(database);
7+
8+
async Task CreateIndexesAsync(IMongoDatabase database)
9+
{
10+
var moviesIndex = new CreateIndexModel<Movie>(Builders<Movie>.IndexKeys
11+
.Ascending(x => x.Title)
12+
.Ascending(x => x.Genres));
13+
await database.GetCollection<Movie>("movies")
14+
.Indexes.CreateOneAsync(moviesIndex);
15+
}
16+
// end-create-index

0 commit comments

Comments
 (0)