NoSQL with Entity Framework Core

34,398

Solution 1

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.

Solution 2

(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;
    

Solution 3

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.

Share:
34,398
Bassam Alugili
Author by

Bassam Alugili

I'm a born researcher and developer; I love to know how things came to be the way they are! My Page My Videos: Entity Framework Tutorial English/Arabic https://www.xing.com/profile/Bassam_Alugili2 https://sites.google.com/site/bassamalugili/c

Updated on June 13, 2020

Comments

  • Bassam Alugili
    Bassam Alugili almost 4 years

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