Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDbContext for data migration in EF core?

Currently I'm trying to find a method to use EF core 2.2 for data migration. After reading the instruction at: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/

I realise that it only support DbContext. At the moment, in my project I have mongoDBContext and IMongoDBContext for my database and I couldn't find any package that support them. I also try using Microsoft.EntityFrameworkCore.Design but it's not helping. Is there any solution for me to use mongoDbContext as the database provider, or any packages that support mongoDB as the database provider?

As for my attempt to create a migration file despite not having DbContext:

From my terminal

dotnet ef migrations add InitialCreate

Error

No DbContext was found in assembly '.Data'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Thank you so much!

like image 392
Việt Tùng Nguyễn Avatar asked Jan 30 '26 23:01

Việt Tùng Nguyễn


1 Answers

MongoDb is schema-less storage.

Entityframework Migrations is use to update schema of the tables. Yes migrations can also run update/delete/insert part of migration but only because Sql is dependent on schema.

In addition EF is not supporting MongoDb since mongo db is not using sql, mongo db has his own api/language.

Yes recent updates of EFCore supports CosmosDb but only because CosmosDB can talk sql

To sum up.

  1. If you need to modify schema probably your MongoDb design is wrong.
  2. If you want to talk to MongoDb use https://docs.mongodb.com/ecosystem/drivers/csharp/
  3. If you are not familiar with MongoDb use what you familiar with
like image 161
Vova Bilyachat Avatar answered Feb 02 '26 15:02

Vova Bilyachat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!