Docs Menu

Docs HomeRealm

Quick Start with Sync - .NET SDK

On this page

  • Overview
  • Import Realm
  • Initialize the App
  • Define Your Object Model
  • Authenticate a User
  • Open a Realm
  • Create, Read, Update, and Delete Objects

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

  • Created an App

  • Enabled Device Sync


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

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 latest version of the Realm package, and then click Add Package.

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

using Realms;
using Realms.Sync;

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

app = App.Create(myRealmAppId);

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

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


Get Schema from UI if Development Mode Disabled

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

Learn more in the documentation for creating a schema.

The following code shows how to define an object model for a Task 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 MapTo attribute so we can use .NET-friendly casing on our property names.

using MongoDB.Bson;
using Realms;
namespace Examples.Models
public class Task : RealmObject
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
public string Partition { get; set; }
public User Assignee { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public enum TaskStatus

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

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

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

await user.LogOutAsync();


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

config = new PartitionSyncConfiguration("myPart", user);
var realm = await Realm.GetInstanceAsync(config);


See also:

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(),
Partition = "myPart"
realm.Write(() =>

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>()
.FirstOrDefault(t => t.Id == testTaskId);
realm.Write(() =>
t.Status = TaskStatus.InProgress.ToString();

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

realm.Write(() =>
←  Quick Start - .NET SDKPlatform and Framework Compatibility - .NET SDK →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.