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. If you don’t have one, see the dashboard quick start for help getting set up.
-
A supported version of C# and the Fauna C# driver. See Installation for driver installation help.
Procedure
-
Navigate to the Fauna Dashboard
Log in to your Fauna account at Fauna Dashboard if you’re not already logged in.
-
Create a new database
Click NEW DATABASE. Select the
Classic
region group. -
Create a new collection
Click NEW COLLECTION. Name your new collection
People
and save it. -
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.
-
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. -
Create a local application
The following instructions assume you are using Visual Studio Code as a development environment. -
Install the .NET SDK
Download and install .NET version 6.0 or newer.
-
Create a new project folder
Create a new folder named
myTest
in VS Code. This folder holds your project code and supporting files. -
Open a terminal window
Open a new terminal window in VS Code.
-
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.
-
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
andProgram.cs
. -
Include the Fauna package with your
.csproj
fileOpen the
myTest.csproj
file and add the following lines above the closing</Project>
tag:<ItemGroup> <PackageReference Include="FaunaDB.Client" Version="4.2.0" /> </ItemGroup>
-
Edit your
Program.cs
fileRemove the placeholder code from the
Program.cs
file and replace it with the following:The above example app uses the
Create
function to create a new document in thePeople
collection. -
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'
-
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.
Next steps
Now that your development environment is set up, you’re ready to start developing more complex applications. The following resources can help.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!