При формировании базы данных (Scaffold-DbContext) EF Core автоматически сопоставляет столбцы типа NUMBER(1,0) с bool.
Чтобы избежать этого, я изменил сопоставление так, чтобы NUMBER(1,0) рассматривалось как int вместо bool.
Однако при попытке запросить объект я получаю следующее ошибка:
[InvalidOperationException] Между типами не определен оператор приведения
'System.Nullable
Код: Выделить всё
1[System.Int32]' and 'System.NullableЗатем я попытался обработать преобразование во время выполнения, чтобы EF Core обрабатывал эти столбцы как int:
Код: Выделить всё
public class PreferIntTypeMappingSource : RelationalTypeMappingSource
{
private readonly RelationalTypeMappingSourceDependencies _relationalDependencies;
private readonly TypeMappingSourceDependencies _dependencies;
public PreferIntTypeMappingSource(TypeMappingSourceDependencies dependencies,
RelationalTypeMappingSourceDependencies relationalDependencies)
: base(dependencies, relationalDependencies)
{
_dependencies = dependencies;
_relationalDependencies = relationalDependencies;
}
protected override RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
{
var storeType = mappingInfo.StoreTypeNameBase?.ToUpperInvariant();
// אם מדובר ב-NUMBER(1,0) – נחזיר INT
if (!string.IsNullOrEmpty(storeType)
&& storeType.StartsWith("NUMBER", StringComparison.OrdinalIgnoreCase)
&& mappingInfo.Scale == 0
&& mappingInfo.Precision.HasValue)
{
int p = mappingInfo.Precision.Value;
if (p >= 1 && p
Подробнее здесь: [url]https://stackoverflow.com/questions/79803528/number1-0-mapped-to-bool-instead-of-int-causes-conversion-and-mapping-errors[/url]
Мобильная версия