Add EnsureEntity<T> method to DbModelOptions

Introduce EnsureEntity<T>(bool isVirtual) to validate and ensure entity options exist for a given type in either Entities or VirtualEntities. Throws an exception if options are missing.
This commit is contained in:
2025-12-10 14:24:45 +01:00
parent f8e7f8c974
commit 009bb623b5

View File

@@ -7,4 +7,17 @@ public record DbModelOptions
public Dictionary<string, EntityOptions> Entities { get; init; } = [];
public Dictionary<string, VirtualEntityOptions> VirtualEntities { get; init; } = [];
public void EnsureEntity<T>(bool isVirtual)
{
var cluster = isVirtual
? VirtualEntities.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as EntityBaseOptions)
: Entities.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as EntityBaseOptions);
if(cluster.TryGetValue(nameof(T), out var entityOptions))
entityOptions.EnsureProperties<T>();
else
throw new InvalidOperationException($"Entity options for type '{typeof(T).FullName}' not found.");
}
}