Path: redb/redb.Core/Exceptions/RedbSchemeMismatchException.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Exceptions/RedbSchemeMismatchException.cs
----
namespace redb.Core.Exceptions;
///
/// Thrown by LoadAsync<TProps> when the loaded object belongs to a different scheme than
/// TProps maps to, and RedbServiceConfiguration.ThrowOnSchemeMismatch is set.
/// By default the mismatch is not thrown — LoadAsync returns null instead, so a
/// soft-deleted object (scheme -10) reads as null. Set the flag to surface a genuine type
/// mistake loudly.
///
/// The check exists because loading an object under the wrong Props type deserializes garbage into the
/// fields and — with EnablePropsCache — caches it under that type, so a following
/// GetWithoutHashValidation would keep returning the garbage. Whether it throws or returns null,
/// the garbage never reaches the cache.
///
///
/// The untyped LoadAsync(objectId) (returning IRedbObject without Props) is never affected.
///
///
public class RedbSchemeMismatchException : Exception
{
/// Id of the object that was loaded.
public long ObjectId { get; }
/// The Props type the caller asked for.
public Type RequestedType { get; }
/// Scheme id that maps to.
public long ExpectedSchemeId { get; }
/// Scheme id the loaded object actually has.
public long ActualSchemeId { get; }
public RedbSchemeMismatchException(long objectId, Type requestedType, long expectedSchemeId, long actualSchemeId)
: base($"Object {objectId} belongs to scheme {actualSchemeId}, but was loaded as " +
$"'{requestedType.FullName ?? requestedType.Name}' which maps to scheme {expectedSchemeId}. " +
"Loading an object under the wrong Props type yields garbage and poisons the cache. " +
"Use the correct TProps, load untyped via LoadAsync(objectId), or unset " +
"RedbServiceConfiguration.ThrowOnSchemeMismatch to get null instead of this exception.")
{
ObjectId = objectId;
RequestedType = requestedType;
ExpectedSchemeId = expectedSchemeId;
ActualSchemeId = actualSchemeId;
}
///
/// The same mismatch met elsewhere than in a load (a save under the id of an object of another scheme): the caller
/// says what happened and what was left untouched.
///
public RedbSchemeMismatchException(long objectId, Type requestedType, long expectedSchemeId, long actualSchemeId,
string message)
: base(message)
{
ObjectId = objectId;
RequestedType = requestedType;
ExpectedSchemeId = expectedSchemeId;
ActualSchemeId = actualSchemeId;
}
}