Reputation: 201
I'm setting up an odata v4 api using .Net + Mongodb.
From microsoft official document, entity framework was used as the data-access layer. But looks like it is not working for mongodb. Is there any replacement for EF that can be used with mongo db?
Upvotes: 0
Views: 1741
Reputation: 1032
There is an open-source project called MongoFramework which aims to be
an "Entity Framework"-like interface for MongoDB.
If you are looking for a ORM that looks and acts like Entity Framework but works with MongoDB, this might be what you want.
Upvotes: 1
Reputation: 14436
It totally depends what features you are wanting from EF to be wrapped around MongoDB, one of the most common things is to be able to use Linq, and because you mentioned OData which uses linq I'll explain that one.
With the standard mongodb driver (https://www.nuget.org/packages/MongoDB.Driver/2.7.2) you can do the following to expose an IQueryable<T>
object on your collections:
var mongoClient = new MongoClient();
var mongoDatabase = mongoClient.GetDatabase("test");
var mongoCollection = mongoDatabase.GetCollection<Person>("users");
var over18 = await mongoCollection.AsQueryable()
.Where(x => x.Age >= 18)
.Select(x => x.Name)
.ToListAsync();
class Person
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Upvotes: 1