23

I need any NoSQL provider for Entity Framework Core. Can I use the EF-Core version with MongoDB /Raven or anything?

2

3 Answers 3

29

Support for NoSQL database providers such as Azure Table Storage, Redis and others (like MongoDb) are still in EF Core team backlog and has not been implemented yet and will not be implemented for Core 1.0.0 release.

That said, according to the EF Core Roadmap, support for NoSQL Database providers is a high priority feature for the team and will be shipped in the future releases after Core 1.0.0 release.

9
  • Thank you I saw the Julie Lerman example about Azure table. so that means I have to wait a little bit or you have any trick to use BrightstarDB? Jun 1, 2016 at 18:58
  • 1
    Her code sample was based on EntityFramework.AzureTableStorage 7.0.0-beta1 nuget package but it has been dropped in RC2 release as you can see here: nuget.org/packages?q=Microsoft.EntityFrameworkCore Jun 1, 2016 at 19:21
  • I think we are out of luck to use EF Core 1.0.0 with NoSQL databases, unfortunately. Jun 1, 2016 at 19:23
  • 3
    @BassamAlugili If you can tolerate a paid solution, CData Software (the company I work for) has created ADO.NET Providers that will allow you to create a EF project with connectivity to NoSQL sources, like MongoDB, Couchbase, or Cassandra. Jun 1, 2016 at 19:48
  • @JerodJohnson Thank you for the info. do you have any kind of a test version something like 30 days. Jun 1, 2016 at 22:34
9

Disclaimer: I am the owner and operator of this open source project.

In case you're still looking for a MongoDB EF-Core provider, you can find my provider on GitHub: EntityFrameworkCore.MongoDB. The project currently includes an EF-Core database provider and an ASP.NET Core Identity provider.

NOTE: the provider is still in preview/prerelease pending proper support for complex types in EF-Core's StateManager.

You can get to the packages by adding the following NuGet source to your project:

nuget sources add -name EFCore-MongoDb -Source https://www.myget.org/gallery/efcore-mongodb

Check out the getting started wiki for a closer look.

2
  • does it support inheritence model without loss of data? Apr 30, 2017 at 16:00
  • If you're asking about polymorphism, then yes, it supports that out-of-the-box. The repo has a sample domain project and a handful of tests that demonstrate this functionality. Please note, however, that the EFCore-MongoDB is still in beta/preview pending proper support for complex types in EF-Core 2.0 (which is the version the project is built against). May 1, 2017 at 16:23
7

(moving comment to answer, so I'm not hijacking @MortezaManavi's answer)

In your question, you reference EF Core. As I mentioned, we have a ADO.NET providers for many NoSQL data sources. You can download a free, 30-day trial (or open beta, depending on the data source) for any of our providers. I've included links to our current NoSQL offerings at the bottom of my answer.

We have an article in our Knowledge Base for connecting to MongoDB Data with EF6 using a code-first approach (though the principles can be applied regardless of the data source). I've transcribed the contents of that article here.


  1. Open Visual Studio and create a new Windows Form Application. This article uses a C# project with .NET 4.5.
  2. Run the command 'Install-Package EntityFramework' in the Package Manger Console in Visual Studio to install the latest release of Entity Framework.
  3. Modify the App.config file in the project to add a reference to the MongoDB Entity Framework 6 assembly and the connection string.

    Set the Server, Database, User, and Password connection properties to connect to MongoDB.

    <configuration>
       ... 
      <connectionStrings>
        <add name=&quot;MongoDBContext&quot; connectionString=&quot;Offline=False;Server=MyServer;Port=27017;Database=test;User=test;&quot; providerName=&quot;System.Data.CData.MongoDB&quot; />
      </connectionStrings>
      <entityFramework>
        <providers>
           ... 
          <provider invariantName=&quot;System.Data.CData.MongoDB&quot; type=&quot;System.Data.CData.MongoDB.MongoDBProviderServices, System.Data.CData.MongoDB.Entities.EF6&quot; />
        </providers>
      <entityFramework>
    </configuration>
    
  4. Add a reference to System.Data.CData.MongoDB.Entities.EF6.dll, located in the lib -> 4.0 subfolder in the installation directory.

  5. Build the project at this point to ensure everything is working correctly. Once that's done, you can start coding using Entity Framework.
  6. Add a new .cs file to the project and add a class to it. This will be your database context, and it will extend the DbContext class. In the example, this class is named MongoDBContext. The following code example overrides the OnModelCreating method to make the following changes:

    • Remove PluralizingTableNameConvention from the ModelBuilder Conventions.
    • Remove requests to the MigrationHistory table.

      using System.Data.Entity;
      using System.Data.Entity.Infrastructure;
      using System.Data.Entity.ModelConfiguration.Conventions;
      
      class MongoDBContext : DbContext {
        public MongoDBContext() { }
      
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          // To remove the requests to the Migration History table
          Database.SetInitializer<MongoDBContext>(null);  
          // To remove the plural names    
          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }  
      }
      
  7. Create another .cs file and name it after the MongoDB entity you are retrieving, for example, Customers. In this file, define both the Entity and the Entity Configuration, which will resemble the example below:

    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    
    [System.ComponentModel.DataAnnotations.Schema.Table("Customers")]
    public class Customers {
      [System.ComponentModel.DataAnnotations.Key]
    
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public System.String _id { get; set; }
      public System.String CompanyName { get; set; }
    }
    
    public class CustomersMap : EntityTypeConfiguration<Customers> {
      public CustomersMap() {
        this.ToTable(&quot;Customers&quot;);
        this.HasKey(Customers => Customers._id);
        this.Property(Customers => Customers.CompanyName);
      }
    }
    
  8. Now that you have created an entity, add the entity to your context class:

    public DbSet<Customers> Customers { set; get; }
    
  9. With the context and entity finished, you are now ready to query the data in a separate class. For example:

    MongoDBContext context = new MongoDBContext();
    context.Configuration.UseDatabaseNullSemantics = true;
    var query = from line in context.Customers select line;
    

5
  • now i getting an error: [500] Could not execute the specified command: execmodel.Rowset.execute(): The collection 'Customers' doesn't exist. any idea Jun 24, 2016 at 12:09
  • 1
    @BassamAlugili I'm happy to help here where I can, but in this case, you'll likely want to reach out to our Support Team, since they're better equipped to help you debug any specific issues you're seeing. Jun 24, 2016 at 12:15
  • You can see my test in added video link in the last 5 mins. youtube.com/watch?v=s2XpXQTcmHM I will write some blogs about nosql with EF. Jul 18, 2016 at 7:23
  • one thing is for me is very important how can I do something like that dbContext.Customers.Include("Customers.Orders"); I need the nested collection in the customer? Jul 18, 2016 at 7:34
  • @BassamAlugili The best answer for this would be to use a static schema. Unfortunately, the way to do this is too involved for a SO comment. Can you reach out to our Support Team? Jul 19, 2016 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.