Couchbase’s new EF Core provider opens the door to some powerful .NET integrations: even ones traditionally tied to relational databases. This post walks through how Identity, GraphQL, and OData all work with Couchbase.

In this post, I’ll walk through three advanced EF Core integrations that I have successfully tested with Couchbase:

    1. ASP.NET Core Identity
    2. GraphQL (via Hot Chocolate)
    3. OData

Note: These integrations are based on limited testing and are not officially supported (yet). Your mileage may vary, but so far, they show a lot of promise.

ASP.NET Core Identity

Microsoft.AspNetCore.Identity.EntityFrameworkCore provides a plug-and-play authentication and user management system for ASP.NET apps.

Couchbase’s EF Core provider works well with it. The only caveat is that you’ll need to make sure the proper collections exist first (like AspNetUsers, AspNetRoles, etc).

Note: You must create the following collections in advance: AspNetUsers, AspNetRoles, AspNetUserRoles, AspNetUserClaims, AspNetUserLogins, AspNetUserTokens, AspNetRoleClaims.

Example EF setup

MVC auth example

Here’s an ASP.NET Core MVC controller with registration, login, and logout, as well as a custom role:

The data follows the standard Identity structure, stored in a Couchbase document. For example, a document in AspNetUser collection:

A document in AspNetUser collection


GraphQL with Hot Chocolate

Hot Chocolate is a popular GraphQL server for .NET. It can integrate with EF Core, leaning on the LINQ capabilities of the provider (which Couchbase has). This means that GraphQL queries are translated to LINQ, which is then translated to Couchbase SQL++.

Setup

Example usage

  • Go to /graphql in browser (this brings up a web interface)

  • Try a query like this:

This will return results like:

Tips

    • GraphQL queries need to match your GSI indexes (it’s just SQL++ queries under the hood).
    • You can use cover indexes and other SQL++ indexes to improve performance.

OData

Microsoft.AspNetCore.OData exposes your EF Core data as OData endpoints, making it easy to connect tools like Excel, Power BI, and Tableau to Couchbase.

Sample program.cs

Controller

Example OData queries

    • https://localhost:7037/odata/Widgets?$filter=price gt 1&$orderby=name
    • https://localhost:7037/odata/Widgets?$select=name,price&$top=10

Note: Make sure your EF Core LINQ queries can be translated to SQL++ and that any filtered/sorted fields are indexed in Couchbase.

Wrapping up

All of these integrations are backed by EF Core—and now, with Couchbase support, you can take full advantage of them in your code. Whether you’re building secure web applications, GraphQL APIs or integrating with BI tools, The EF Core and Couchbase combo makes it possible.

Curious to see more? Let us know what integrations you’d like to explore next!

Author

Posted by Matthew Groves

Matthew D. Groves is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding professionally ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Senior Product Marketing Manager for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET, Pro Microservices in .NET, a Pluralsight author, and a Microsoft MVP.

Leave a reply