Navigation

Quick Start

Overview

This page contains information to quickly get Realm Database integrated into your .NET app. Before you begin, ensure you have:

Note

See Supported Platforms for the list of platforms that Realm supports.

Import Realm

In the shared code project of your solution, go to Manage NuGet Packages. In the NuGet Package Manager, switch to the Browse tab and then search for Realm. Select The Realm driver and then click Add Package.

At the top of each C# file that uses Realm, add the following using statement:

using Realms;

Initialize the App

To use MongoDB Realm features such as authentication and sync, you must access your Realm app using your Realm app ID. You can find your Realm app ID in the Realm UI. With that, you can initialize the realm app, as shown in the following code:

app = App.Create(myRealmAppId);

Define Your Object Model

Your application’s object model defines the data that you can store within Realm Database and synchronize to and from MongoDB Realm.

If have not enabled Realm Sync or you enabled Sync with Development Mode in the Realm UI, you can define your object model directly in code.

Note

If you have enabled Sync but turned off Developer Mode, you can copy and paste the object model definitions that Realm generated for you from the SDKs tab in the Realm UI. You must re-enable Developer Mode if you want to make changes to the object model definition from client side code. See Configure Your Data Model.

The following code shows how to define an object model for a RealmTask object. In this example, we have marked the Id field as the Primary Key, marked the Name and Status properties as “required”, and are using the BsonElement attribute so we can use .NET-friendly casing on our property names.

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Realms;

namespace dotnet
{
    public class Task : RealmObject
    {
        [PrimaryKey]
        [BsonElement("_id")]
        public ObjectId _id { get; set; }

        [BsonElement("_partition")]
        public string Partition { get; set; }

        [BsonElement("assignee")]
        public User Assignee { get; set; }

        [BsonElement("name")]
        [Required]
        public string Name { get; set; }

        [BsonElement("status")]
        [Required]
        public string Status { get; set; }

        public Task()
        {
            this._id = ObjectId.GenerateNewId();
        }
    }

    public enum TaskStatus
    {
        Open,
        InProgress,
        Complete
    }
}

Authenticate a User

After you have enabled anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information:

var user = app.LogInAsync(Credentials.Anonymous()).Result;

You should also provide a way for the user to log out. The following code shows how to do this by calling LogOutAsync():

await realmUser.LogOutAsync();

Open a Realm

Once you have enabled Realm Sync and authenticated a user, you can open a synced realm. Use the SyncConfiguration to control the specifics of how your application synchronizes data with MongoDB Realm, which might include the partition name, whether to wait for an initial batch of data to sync, and more.

config = new SyncConfiguration("myPartition", user);
var realm = await Realm.GetInstanceAsync(config);

See also

Sync Data

Create, Read, Update, and Delete Objects

Once you have opened a realm, you can modify the objects in it. All changes to the data (creates, writes, and updates) must occur within a Write transaction block.

The following code shows how to create a new Task. You instantiate the Task class and then add the new Task to the realm in a write block:

var testTask = new Task
{
    Name = "Do this thing",
    Status = TaskStatus.Open.ToString()
};

realm.Write(() =>
{
    realm.Add(testTask);
});

The following code demonstrates how to retrieve a live collection of all Task objects in the realm:

var tasks = realm.All<Task>();

You can also filter that collection using Linq. This example returns all Tasks that have a status of “Open”:

tasks = realm.All<Task>().Where(t => t.Status == "Open");

To modify a task, you update its properties in a write transaction block:

var t = realm.All<Task>()
    .Where(t => t._id == testTaskId)
    .FirstOrDefault();

realm.Write(() =>
{
    t.Status = TaskStatus.InProgress.ToString();
});

Finally, you can delete a task by calling the Remove() method:

realm.Write(() =>
{
    realm.RemoveAll<Task>();
});