Movatterモバイル変換


[0]ホーム

URL:


Docs Menu

Object Models & Schemas - .NET SDK

Atlas Device SDKs are deprecated. Refer to thedeprecation page for details.

Object Types & Schemas

Realm applications model data as objects composed of property-value pairsthat each contain one or more primitive data types or other Realmobjects. Realm objects are essentially the sameas regular objects, but they inherit fromRealmObject orEmbeddedObject and includeadditional features likereal-time updating data views and reactivechange event handlers.

Every Realm object has anobject type that refers to the object'sclass. Objects of the same type share anobject schema that defines the properties and relationships of thoseobjects.

Schemas

In C#, you typically define object schemas by using the C# class declarations.When Realm is initialized, it discovers theRealm objects defined in all assemblies that have been loaded andgenerates schemas accordingly. This is the simplest approach to defining aschema, and is generally the least error-prone. However, this approach includesall loaded Realm objects, and there may be cases where you only wantto usea subset of classes, or tocustomize Realm object schemas. To do this, you canprogrammatically define a schema.

Note

.NET does not load an assembly until you reference a class in it, so if youdefine your object models in one assembly and instantiate Realmin another, be sure to call a method in the assembly that contains the objectmodelsbefore initialization. Otherwise, Realm will not discoverthe objects when it first loads.

Working with Realm Objects

The following code block shows an object schema that describes a Dog.Every Dog object must include aName and mayoptionally include the dog'sAge,Breed and a list of people thatrepresents the dog'sOwners.

publicpartialclassDog :IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id {get;set; }
publicstring Name {get;set; }
publicint Age {get;set; }
publicstring Breed {get;set; }
public IList<Person> Owners {get; }
}
publicpartialclassPerson :IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id {get;set; }
publicstring Name {get;set; }
// etc...
/* To add items tothe IList<T>:
var dog = new Dog();
var caleb = new Person { Name = "Caleb" };
dog.Owners.Add(caleb);
*/
}

Note

To define a collection of objects within an object, use anIList<T> withonly a getter. You do not need to initialize it in the constructor, as realmwill generate a collection instance the first time the property is accessed.

Note

Further Examples

TheCRUD - .NET SDK section provides examples of creating, reading,updating, filtering, and deleting Realm objects.


[8]ページ先頭

©2009-2025 Movatter.jp