Docs Menu

Docs HomeRealm

Manually Define a Schema - .NET SDK

On this page

  • Use the Schema Property

When Realm processes Realm objects, 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.

Important

Inheritance

All Realm objects inherit from the IRealmObject, IEmbeddedObject, or IAsymmetricObject interface and should be declared partial classes.

You can also derive from the RealmObject, EmbeddedObject, or AsymmetricObject base classes. However, in the future we may deprecate the base classes. You should use the interfaces for any new classes that you write.

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 Builder("ClassA", ObjectType.EmbeddedObject)
{
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() }
};
←  Embedded Objects - .NET SDKRelationships - .NET SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

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