Mongo EF Core provider supports index creation?

Does the Mongo EF Core provider support index creation? For simple model creation like:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  modelBuilder.Entity<Invoice>()
    .ToCollection("invoices")
    .HasIndex(i => i.Name);
}
`
...  I would have expected an index to be created. It is not. I don't see any mention of indexes in the EF Core Provider features/limitations list.

Hi @Steve_Rinn,

Welcome to the MongoDB Community forums! Index creation is not directly supported since our EF Core provider isn’t exposing admin type functionality. However, since our EF Core provider is built on top of the .NET/C# driver, you can re use the MongoClient used to create the DbContext and perform index management as you normally would with the driver. Here’s a sample code snippet of that and a talk that explains the same. Hope that helps.

Thanks,

Rishit.

1 Like

No feedback from anyone on this?

Apologies for the delay Steve. Our team keeps an eye on the dotnet and efcore tags so posts that aren’t tagged with either of these tend to get overlooked.

Right now the relational providers in EF use HasIndex in their schema migration strategies so it doesn’t map well to schemaless document database providers like ours.

The EF team is looking at what they could do with HasIndex for document databases in the EF-10 timeframe and we’ll take some cues from there as to what we can do (hopefully sooner) in our provider.

1 Like

Thanks for the reply, Damien, and no worries. I ought to have tagged the post, but since I’m new to the forum, I didn’t know how to create tags. Like the previous poster suggested, we had already coded around this one via the C# / dot-net driver. I was just surprised the supported features/limitations README in the repo was silent on index creation since it’s such a typical task.

I’ll add it to the Readme. I was considering it part of Migrations which is listed in unsupported but that requires people know that HasIndex is part of the EF migrations system which isn’t obvious.

Thanks for answering, you made my day.

1 Like