Skip to content

couchbaselabs/couchbase-efcore-provider

Repository files navigation

EF Core Couchbase DB Provider

This database provider allows Entity Framework Core to be used with Couchbase Database. The provider is maintained as part of the Couchbase EFCore Project.

It is strongly recommended to familiarize yourself with the Couchbase Database documentation before reading this section. The EF Core Couchbase Db Provider, works with Couchbase Server and Couchbase Capella DBaaS.

Note

The EF Core Couchbase DB Provider is currently in developer preview and not all code paths work as of the writing of this document.

Install

Install the Couchbase.EntityFrameworkCore NuGet package.

.NET Core CLI or Jet Brains Rider IDE

dotnet add package Couchbase.EntityFrameworkCore

Visual Studio

Install-Package Couchbase.EntityFrameworkCore

Get Started

Tip

You can view this article's sample on GitHub

As for other providers the first step is to call UseCouchbase:

protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseCouchbase<INamedBucketProvider>(new ClusterOptions()
                .WithCredentials("Administrator", "password")
                .WithConnectionString("couchbase://localhost"),
            couchbaseDbContextOptions =>
            {
                couchbaseDbContextOptions.Bucket = "OrdersDB";
                couchbaseDbContextOptions.Scope = "_default";
            });

In this example Order is a simple entity with a reference to the owned type StreetAddress.

public class Order
{
    public int Id { get; set; }
    public int? TrackingNumber { get; set; }
    public string PartitionKey { get; set; }
    public StreetAddress ShippingAddress { get; set; }
}
public class StreetAddress
{
    public string Street { get; set; }
    public string City { get; set; }
}

Saving and querying data follows the normal EF pattern:

using (var context = new OrderContext())
{
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();

    context.Add(
        new Order
        {
            Id = 1, ShippingAddress = new StreetAddress { City = "London", Street = "221 B Baker St" }, PartitionKey = "1"
        });

    await context.SaveChangesAsync();
}

using (var context = new OrderContext())
{
    var order = await context.Orders.FirstAsync();
    Console.WriteLine($"First order will ship to: {order.ShippingAddress.Street}, {order.ShippingAddress.City}");
    Console.WriteLine();
}

Couchbase DB options

There exists options for both the Couchbase SDK which the Couchbase EF Core DB Provider uses and for the provider itself.

What works:

  • Basic projections/queries
  • Some SQL++ functions - COUNT, CONTAINS, etc
  • Basic CRUD and change tracking

What doesn't work

  • Eager Loading
  • Most all SQL++ functions
  • Value generation
  • META, RYOW, etc
  • Lots...it's a WIP

Documentation

About

An EF Core DB Provider for Couchbase NoSQL database written in CSharp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages