.NET

3 EF Core Integrations That Work with Couchbase

5 MIN DE LEITURA

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

Nota: 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).

Nota: 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

Nota: 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!

Compartilhe este artigo

Autor

Matthew D. Groves é um cara que adora programar. Não importa se é C#, jQuery ou PHP: ele envia pull requests para qualquer coisa. Ele vem programando profissionalmente desde que escreveu um aplicativo de ponto de venda em QuickBASIC para a pizzaria dos pais, lá nos anos 90. Atualmente, trabalha como gerente sênior de marketing de produto na Couchbase. Passa seu tempo livre com a família, assistindo aos Reds e participando da comunidade de desenvolvedores. É autor dos livros “AOP in .NET” e “Pro Microservices in .NET”, autor da Pluralsight e MVP da Microsoft.

Deixe um comentário

Pronto para começar com o Couchbase Capella?

Começar a construir

Confira nosso portal para desenvolvedores para explorar o NoSQL, navegar por recursos e começar com tutoriais.

Use o Capella free

Coloque a mão na massa com o Couchbase em apenas alguns cliques. O Capella DBaaS é a maneira mais fácil e rápida de começar.

Entre em contato

Quer saber mais sobre as ofertas do Couchbase? Deixe-nos ajudar.