79

Is it possible that Entity Framework supports MongoDB database? Has someone written an Entity Framework MongoDB Provider?

1

9 Answers 9

60

Short answer - no, it's for sure possible, but not reasonable.

MongoDB is document database and not support any physical relations between collections. EF is a good fit for relational databases like SQL, MySQL, etc. MongoDB works faster with embedded documents. So it's just two different approaches.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not? Entity Framework is an ORM and a complex composite object is a lot more similar to a document than a to bunch of rows in several tables liked by few FKs. Besides, EF to MongoDB could rely on manual references to implement relations. So implementing a EF provider to a document DB should be even easier than to a RDBMS.
Nowadays EF Core is not bound to relational databases, and does support document databases as well. For example, there is a provider for Azure Cosmos DB document database.
EF is an ORM, that maps Objects in application tire to Relational Model in Data tire. In mongo with the help of Mongo drivers Objects can be directly passed to Data tire, Hence no ORM is required. Using EF for mongodb or develop and Provider is overkill.
I think this will make things clear: mongodb.com/docs/entity-framework/current
You gotta love how confidently wrong a lot of top voted answers are on SO. MongoDb is now officially supported nuget.org/packages/MongoDB.EntityFrameworkCore
48

Check out the latest version of the MongoDB C# Library.... supports LINQ!

https://mongodb.github.io/mongo-csharp-driver/1.11/linq/

3 Comments

supporting Linq does that means it is also supporting EF?
@mehul9595 That's a good question, and no, it doesn't mean that, so this answer really isn't an answer to the question that's asked at all.
will LINQ be useful as mongodb api supposedly returns json data ?
8

I'm a big fan of entity framework, so when I first tried out mongoDb I've created a library called "MongoDB.Dynamic" that has an approach next to EF Code First. MongoDB has no relations between collections physically, but you can simulate it. MongoDB.Dynamic has a feature known as Eager Loading that you can configure relationships between collections and load related documents automatically if an entity have the conventioned structure. You can try and read some documentation at MongoDB.Dynamic

Comments

5

MongoDB has now released an officially supported EF Core provider (currently in preview).

Github repo: https://github.com/mongodb/mongo-efcore-provider

NuGet: https://www.nuget.org/packages/MongoDB.EntityFrameworkCore

Issue/bug tracker: https://jira.mongodb.org/projects/EF/issues/EF-63

Comments

2

Looking online, RSSBus look to have created a MongoDBProviderServices dll that could be used with entity framework 6.

http://cdn.rssbus.com/help/DG1/ado/pg_ef6.htm

The link includes a tutorial on how to implement it.

Comments

2

CData developed a library for doing CRUD operations with MongoDb using Entity Framework 6.0. But its not free.

Details of the library can be found in

https://www.cdata.com/kb/tech/mongodb-ado-codefirst.rst

Comments

0

I do not suggest to use the old version of MongoDb provider that uses LINQ. The best and stable C# MongoDb driver is the official MongoDb driver 2.0 for C# that uses Builders mechanics.

The LINQ legacy driver has many constraints with inconvenient and slow Bulk insert mechanism.

3 Comments

Hi DotNetGoose - your answer looks useful, but a little confusing; could you add some links to back up what you say, for example to the official documentation of the MongoDB driver for C?
@VinceBowdren Hi, i made that conclusion based on my own experience of working with old and new MongoDb driver. I don't remember details, it was a year ago, but old driver have slower performance, very uncomfortable mechanics of bulk operations and some bugs and unobvious behaviour.
I know this is an old answer, but LINQ support for the official driver has been rewritten as of version 2.1 (Oct 2015), so it's the recommended approach if you would like to use LINQ now. See the release notes
0

Rachelle from MongoDB here. Yes, you can use the Entity Framework with MongoDB. Our C# team just released our official provider, the documentation is located here https://www.mongodb.com/docs/entity-framework/current/ . Please reach out if you have feedback or questions!

Comments

-2

I am not sure, but I think the Microsoft.EntityFrameworkCore.Cosmos Database Provider will work for you.

3 Comments

The question is obviously for Mongo DB. You answer is for Cosmos DB. Idk what to say...
Well I already said I am not sure. I suggested the use of the driver because Cosmos supports MongoDB's wire protocol.
Comos supports Mongo API - so you can have Mongo Driver in your app and use cosmos as your DB, but not vice versa (cosmos driver with mongo db)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.