Navigation

Quick Start with Sync - .NET SDK

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

Info With Circle IconCreated with Sketch.Note

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;

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);

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.

Info With Circle IconCreated with Sketch.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 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 dotnet
{
public class Task : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("_partition")]
public string Partition { get; set; }
[MapTo("assignee")]
public User Assignee { get; set; }
[MapTo("name")]
[Required]
public string Name { get; set; }
[MapTo("status")]
[Required]
public string Status { get; set; }
}
public enum TaskStatus
{
Open,
InProgress,
Complete
}
}

After you have enabled anonymous authentication in the Realm 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();
Bulb IconTip
See also:

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("myPart", user);
var realm = await Realm.GetInstanceAsync(config);
Bulb IconTip
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()
};
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>()
.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(() =>
{
realm.Remove(myTask);
});
Give Feedback