Docs Menu

Manually Define a Schema - .NET SDK

On this page

  • Use the Schema Property

When Realm processes Realm objects (C# classes that inherit from RealmObject or EmbeddedObject), it generates a schema for each class based on the class properties. However, there may be times that you want to manually define the schema, and the .NET SDK provides a mechanism for doing so.

You use the Schema property of the RealmConfigurationBase object to control how schemas are defined. The following code example shows three ways to do this, from easiest to most complex: automatic configuration, manual configuration, and a mix of both methods.

// By default, all loaded RealmObject classes are included.
// Use the RealmConfiguration when you want to
// construct a schema for only specific C# classes:
var config = new RealmConfiguration
{
Schema = new[] { typeof(ClassA), typeof(ClassB) }
};
// More advanced: construct the schema manually
var manualConfig = new RealmConfiguration
{
Schema = new RealmSchema.Builder
{
new ObjectSchema.Builder("ClassA", isEmbedded: false)
{
Property.Primitive("Id",
RealmValueType.Guid,
isPrimaryKey: true),
Property.Primitive("LastName",
RealmValueType.String,
isNullable: true,
isIndexed: true)
}
}
};
// Most advanced: mix and match
var mixedSchema = new ObjectSchema.Builder(typeof(ClassA));
mixedSchema.Add(Property.FromType<int>("ThisIsNotInTheCSharpClass"));
// `mixedSchema` now has all of the properties of the ClassA class
// and an extra integer property called "ThisIsNotInTheCSharpClass"
var mixedConfig = new RealmConfiguration
{
Schema = new[] { mixedSchema.Build() }
};
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.