Getting Started with Couchbase EFCore
Get up and running with Couchbase.EntityFrameworkCore
via a Console application.
The Contoso University sample application is also a great way to become familier with the provider.
This is a clone of the EFGettingStarted example in the MS Docs. |
Prerequisites
-
Download and Install .NET 8
-
Create a Couchbase Capella free tier account.
Create the Couchbase Capella database
Checkout this blog for details on how to set up the Couchbase Capella database for this example. |
After you have created your Couchbase Capella free tier, you must create a Cluster, a Bucket named "Content", a Scope named "Blogs", and then Collections called "Blog" and "Post" for storing the documents. Note that names are case-sensitive.
Create a Console application
-
Create the .NET Console Application using this tutorial or Visual Studio or via the Command Line:
mkdir CouchbaseGettingStarted cd CouchbaseGettingStarted dotnet new console
-
Once you have created the console application add the dependency on
Couchbase.EntityFrameworkCore
:dotnet install Couchbase.EntityFrameworkCore --version 1.0.0
-
Add the dependency on
EFCore.NamingConventions
:dotnet install EFCore.NamingConventions --version 8.0.3
The EFCore.NamingConventions package is used to enforce the casing of the SQL++ statements generated by EF Core.
The default casing for the SQL generated by EF Core is PascalCase, which doesn’t match the JSON CamelCase convention used by the Couchbase SDK.
If the two do not match, results will be returned, but the values will be the .NET defaults for each respective property type.
|
-
In the project directory, create Model.cs with the following code:
using Microsoft.EntityFrameworkCore; using Couchbase; using Couchbase.EntityFrameworkCore; using Couchbase.EntityFrameworkCore.Extensions; using Couchbase.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) { var loggingFactory = LoggerFactory.Create(builder => builder.AddConsole()); options.UseCouchbase( new ClusterOptions() .WithCredentials("USERNAME", "PASSWORD") .WithConnectionString("couchbases://cb.xxxxxxxx.cloud.couchbase.com") .WithLogging(loggingFactory), couchbaseDbContextOptions => { couchbaseDbContextOptions.Bucket = "Content"; couchbaseDbContextOptions.Scope = "Blogs"; }); optons.UseCamelCaseNamingConvention(); // Use EFCore.NamingConventions for JSON document casing } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>().ToCouchbaseCollection(this, "Blog"); modelBuilder.Entity<Post>().ToCouchbaseCollection(this, "Post"); } } public class Blog { public string BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; } = new(); } public class Post { public string PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
Create, read, update & delete
-
Open Program.cs and replace the contents with the following code
using var db = new BloggingContext(); // Note: This sample requires the Couchbase database to be created before running. // The Bucket name is "Content", the scope is "Blogs" and the collections are "Post and "Blog" // Buckets, Scopes and Collections are case sensitive! // Create Console.WriteLine("Inserting a new blog"); var blog = new Blog { Url = "http://blogs.msdn.com/adonet", BlogId = Guid.NewGuid().ToString() }; db.Add(blog); await db.SaveChangesAsync(); // Read Console.WriteLine("Querying for a blog"); blog = await db.Blogs .OrderBy(b => b.BlogId) .FirstAsync(); // Update Console.WriteLine("Updating the blog and adding a post"); blog.Url = "https://devblogs.microsoft.com/dotnet"; blog.Posts.Add( new Post { Title = "Hello World", Content = "I wrote an app using EF Core!", PostId = Guid.NewGuid().ToString() }); await db.SaveChangesAsync(); // Delete Console.WriteLine("Delete the blog"); db.Remove(blog); await db.SaveChangesAsync();
Run the app
-
Run 'CouchbaseGettingStarted' in Rider IDE Or,
-
Debug > Start Without Debugging in VS Or,
-
dotnet run
in .NET CLI/source/couchbase-dotnet-ef/samples/CouchbaseGettingStarted/bin/Debug/net8.0/CouchbaseGettingStarted Inserting a new blog Querying for a blog Updating the blog and adding a post Delete the blog Process finished with exit code 0.