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 the Couchbase.EntityFrameworkCore NuGet package.
dotnet add package Couchbase.EntityFrameworkCore
Install-Package Couchbase.EntityFrameworkCore
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();
}
- The Contoso University web app code
- Contoso University sample documentation
- Getting started - in more detail
There exists options for both the Couchbase SDK which the Couchbase EF Core DB Provider uses and for the provider itself.
- ClusterOptions settings
- Couchbase EF Core DB Provider settings
- Basic projections/queries
- Some SQL++ functions - COUNT, CONTAINS, etc
- Basic CRUD and change tracking
- Eager Loading
- Most all SQL++ functions
- Value generation
- META, RYOW, etc
- Lots...it's a WIP