C#

This section is intended to help C# developers get started using the Fauna C# driver for application development.

Current version and repository location

Current stable version

4.2.0

Repository

This section provides a high-level overview of working with the driver. For details on the driver’s API, see its documentation.

Installation

Install the Nuget package by adding the package reference to your MSBuild project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="FaunaDB.Client" Version="4.2.0" />
    </ItemGroup>
</Project>

or by using your IDE and searching for FaunaDB.Client.

Example application

The following example code runs a bare-bones C# application which creates a new document in a collection called People.

Prerequisites

  • A Fauna account.

  • A supported version of C# and the Fauna C# driver. See Installation for driver installation help.

Procedure

  1. Navigate to the Fauna v4 Dashboard

    Log in to your Fauna account at v10 Fauna Dashboard if you’re not already logged in.

  2. Create a new database

    Create a new database and select your Region Group.

  3. Access the v4 Dashboard

    Access the Fauna v4 Dashboard by clicking the v4 Dashboard tab for the database.

  4. Create a new collection

    Under Collections, click NEW COLLECTION. Name your new collection People and save it.

  5. Create an access key

    Click SECURITY in the left-side navigation menu. Create a new key for your database. Be sure to save the key’s secret in a safe place, as it is only displayed once.

  6. Create a local environment variable with your access key’s secret

    On MacOS and Linux systems, enter the following in a terminal window:

    export FAUNADB_SECRET=<your-secret>

    For Windows systems, enter the following in a terminal window:

    set FAUNADB_SECRET=<your secret>

    For either example, replace <your secret> with the secret for the access key that you created.

  7. Create a local application

    The following instructions assume you are using Visual Studio Code as a development environment.
  8. Install the .NET SDK

    Download and install .NET version 6.0 or newer.

  9. Create a new project folder

    Create a new folder named myTest in VS Code. This folder holds your project code and supporting files.

  10. Open a terminal window

    Open a new terminal window in VS Code.

  11. Check your .NET version

    To be sure .NET is installed and accessible enter the following command at the terminal prompt:

    dotnet --version

    If .NET is correctly installed on your system, you should see your .NET SDK version number.

  12. Create a new .NET project

    Enter the following command to create a new .NET project:

    dotnet new console

    You should now have some new files in your project folder, including myTest.csproj and Program.cs.

  13. Include the Fauna package with your .csproj file

    Open the myTest.csproj file and add the following lines above the closing </Project> tag:

    <ItemGroup>
      <PackageReference Include="FaunaDB.Client" Version="4.2.0" />
    </ItemGroup>
  14. Edit your Program.cs file

    Remove the placeholder code from the Program.cs file and replace it with the following:

    namespace Example
    {
        using System;
        using System.Threading.Tasks;
        using FaunaDB.Client;
        using FaunaDB.Collections;
        using FaunaDB.Query;
        using FaunaDB.Types;
    
        using static FaunaDB.Query.Language;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                var secret = System.Environment.GetEnvironmentVariable("FAUNADB_SECRET");
                if (secret == null)
                {
                    Console.WriteLine("The FAUNADB_SECRET environment is not set... exiting.");
                    System.Environment.Exit(1);
                }
    
                var endpoint = System.Environment.GetEnvironmentVariable("FAUNADB_ENDPOINT");
                if (endpoint == null)
                {
                    endpoint = "https://db.fauna.com/";
                }
    
                Run(endpoint, secret).Wait();
                System.Environment.Exit(0);
            }
    
            public static async Task Run(string endpoint, string secret)
            {
                var client = new FaunaClient(
                    endpoint: endpoint,
                    secret: secret
                );
    
                try
                {
                    Value result = await client.Query(
                        Create(
                            Collection("People"),
                            Obj(
                                "data", Obj(
                                    "first", "Linus",
                                    "last", "Torvalds",
                                    "age", 52
                                )
                            )
                        )
                    );
                    Console.WriteLine(result);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"ERROR: {e.Message}");
                }
            }
        }
    }

    The above example app uses the Create function to create a new document in the People collection.

  15. Check your environment variable

    At the terminal prompt, enter the following command:

    echo $FAUNADB_SECRET

    You should see the key secret for your Fauna access key. If not, set the environment variable with the following command:

    export FAUNADB_SECRET='your-secret'
  16. Run your program

    At the terminal prompt, enter the following command:

    dotnet run

    You should see results similar to this:

    ObjectV(ref: RefV(id = "280491264236847616", collection = RefV(id = "People", collection = RefV(id = "collections"))),ts: LongV(1603756164680000),data: ObjectV(first: StringV(Linus),last: StringV(Torvalds),age: LongV(52)))

    If you get an error, check to make sure your access key is correct.

\