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)
isPrimaryKey: true),
isNullable: true,
isIndexed: true)
// Most advanced: mix and match
var mixedSchema = new ObjectSchema.Builder(typeof(ClassA));
// `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() }
←  Advanced Guides - .NET SDKThreading - .NET SDK →
Give Feedback
© 2022 MongoDB, Inc.


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