diff --git a/Realm/Realm/DatabaseTypes/Accessors/ManagedAccessor.cs b/Realm/Realm/DatabaseTypes/Accessors/ManagedAccessor.cs index 3b212397d8..a576e8cf85 100644 --- a/Realm/Realm/DatabaseTypes/Accessors/ManagedAccessor.cs +++ b/Realm/Realm/DatabaseTypes/Accessors/ManagedAccessor.cs @@ -84,17 +84,7 @@ protected ManagedAccessor() #pragma warning restore CS8618 { _hashCode = new(() => ObjectHandle!.GetObjHash()); - _objectSchema = new(() => - { - if (Realm!.Config.RelaxedSchema) - { - var objSchemaCopy = Metadata!.Schema.GetBuilder().Build(); - objSchemaCopy.ObjectHandle = ObjectHandle; - return objSchemaCopy; - } - - return Metadata!.Schema; - }); + _objectSchema = new(() => Realm!.Config.RelaxedSchema ? Metadata!.Schema.MakeCopyWithHandle(ObjectHandle!) : Metadata!.Schema); } [MemberNotNull(nameof(Realm), nameof(ObjectHandle), nameof(Metadata))] diff --git a/Realm/Realm/Schema/ObjectSchema.cs b/Realm/Realm/Schema/ObjectSchema.cs index cca0b69c65..3f8db277a6 100644 --- a/Realm/Realm/Schema/ObjectSchema.cs +++ b/Realm/Realm/Schema/ObjectSchema.cs @@ -111,6 +111,15 @@ internal ObjectSchema(string name, ObjectType schemaType, IDictionary new(this, handle); + /// /// A mutable builder that allows you to construct an instance. /// diff --git a/Tests/Realm.Tests/Database/DynamicAccessTests.cs b/Tests/Realm.Tests/Database/DynamicAccessTests.cs index 00811cf8a4..39a72814df 100644 --- a/Tests/Realm.Tests/Database/DynamicAccessTests.cs +++ b/Tests/Realm.Tests/Database/DynamicAccessTests.cs @@ -17,7 +17,6 @@ //////////////////////////////////////////////////////////////////////////// using System; -using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using Microsoft.CSharp.RuntimeBinder; @@ -480,6 +479,7 @@ public void GetProperty_WhenPropertyIsMissing_Throws() RunTestInAllModes((realm, _) => { var allTypesObject = realm.Write(() => realm.DynamicApi.CreateObject(nameof(AllTypesObject))); + Assert.Throws(() => allTypesObject.DynamicApi.Get("idontexist")); }); } @@ -572,6 +572,7 @@ public void SetProperty_WhenPropertyIsMissing_Throws() realm.Write(() => { var ato = realm.DynamicApi.CreateObject(nameof(AllTypesObject)); + Assert.Throws(() => ato.DynamicApi.Set("idontexist", "foo")); }); });