diff --git a/src/StronglyTypedIds.Templates/int-dapper.typedid b/src/StronglyTypedIds.Templates/int-dapper.typedid index 277a4e1f..8a10bf16 100644 --- a/src/StronglyTypedIds.Templates/int-dapper.typedid +++ b/src/StronglyTypedIds.Templates/int-dapper.typedid @@ -12,7 +12,9 @@ return value switch { int intValue => new PLACEHOLDERID(intValue), - long longValue when longValue < int.MaxValue => new PLACEHOLDERID((int)longValue), + short shortValue => new PLACEHOLDERID(shortValue), + long longValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)longValue), + decimal decimalValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)decimalValue), string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new PLACEHOLDERID(result), _ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"), }; diff --git a/src/StronglyTypedIds.Templates/int-full.typedid b/src/StronglyTypedIds.Templates/int-full.typedid index 4cf8cbbe..43f47209 100644 --- a/src/StronglyTypedIds.Templates/int-full.typedid +++ b/src/StronglyTypedIds.Templates/int-full.typedid @@ -241,7 +241,9 @@ return value switch { int intValue => new PLACEHOLDERID(intValue), - long longValue when longValue < int.MaxValue => new PLACEHOLDERID((int)longValue), + short shortValue => new PLACEHOLDERID(shortValue), + long longValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)longValue), + decimal decimalValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)decimalValue), string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new PLACEHOLDERID(result), _ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"), }; diff --git a/src/StronglyTypedIds.Templates/long-dapper.typedid b/src/StronglyTypedIds.Templates/long-dapper.typedid index 3310653e..9466a251 100644 --- a/src/StronglyTypedIds.Templates/long-dapper.typedid +++ b/src/StronglyTypedIds.Templates/long-dapper.typedid @@ -14,6 +14,7 @@ long longValue => new PLACEHOLDERID(longValue), int intValue => new PLACEHOLDERID(intValue), short shortValue => new PLACEHOLDERID(shortValue), + decimal decimalValue and < long.MaxValue and > long.MinValue => new PLACEHOLDERID((long)decimalValue), string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new PLACEHOLDERID(result), _ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"), }; diff --git a/src/StronglyTypedIds.Templates/long-full.typedid b/src/StronglyTypedIds.Templates/long-full.typedid index e6c61cc9..4ce1aaff 100644 --- a/src/StronglyTypedIds.Templates/long-full.typedid +++ b/src/StronglyTypedIds.Templates/long-full.typedid @@ -242,6 +242,7 @@ long longValue => new PLACEHOLDERID(longValue), int intValue => new PLACEHOLDERID(intValue), short shortValue => new PLACEHOLDERID(shortValue), + decimal decimalValue and < long.MaxValue and > long.MinValue => new PLACEHOLDERID((long)decimalValue), string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new PLACEHOLDERID(result), _ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"), }; diff --git a/test/IntegrationLibraries.props b/test/IntegrationLibraries.props index 148bedfd..3d1d2116 100644 --- a/test/IntegrationLibraries.props +++ b/test/IntegrationLibraries.props @@ -30,6 +30,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs index 49406f96..277b9954 100644 --- a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Data.SqlClient; using System.Linq; using System.Text.Json; using System.Threading.Tasks; @@ -282,6 +283,27 @@ public async Task WhenDapperValueConverterUsesValueConverter() Assert.Equal(new ConvertersIntId(123), value); } + [Fact(Skip = "Requires localdb to be available")] + public async Task WhenDapperValueConverterUsesValueConverterWithSqlServer() + { + using var connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30"); + await connection.OpenAsync(); + + var results = await connection.QueryAsync("SELECT CAST (123 AS numeric(38,0))"); + + var value = Assert.Single(results); + Assert.Equal(new ConvertersIntId(123), value); + } + + [Fact] + public void WhenDapperValueConverterAndDecimalUsesValueConverter() + { + var handler = new ConvertersIntId.DapperTypeHandler(); + var value = handler.Parse((decimal) 123L); + + Assert.Equal(new ConvertersIntId(123), value); + } + [Theory] [InlineData(123)] [InlineData("123")] diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs index e7559da4..f2e18289 100644 --- a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Data.SqlClient; using System.Linq; using System.Text.Json; using System.Threading.Tasks; @@ -294,6 +295,27 @@ public async Task WhenDapperValueConverterUsesValueConverter() Assert.Equal(value, new ConvertersLongId(123)); } + [Fact(Skip = "Requires localdb to be available")] + public async Task WhenDapperValueConverterUsesValueConverterWithSqlServer() + { + using var connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30"); + await connection.OpenAsync(); + + var results = await connection.QueryAsync("SELECT CAST (123 AS numeric(38,0))"); + + var value = Assert.Single(results); + Assert.Equal(new ConvertersIntId(123), value); + } + + [Fact] + public void WhenDapperValueConverterAndDecimalUsesValueConverter() + { + var handler = new ConvertersLongId.DapperTypeHandler(); + var value = handler.Parse((decimal) 123L); + + Assert.Equal(new ConvertersLongId(123), value); + } + #if NET6_0_OR_GREATER [Fact] public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()