DataObjects-NET / dataobjects-net

https://dataobjects.net
MIT License
60 stars 23 forks source link

System.InvalidCastException when using Enum : short #303

Open IgoR-NiK opened 1 year ago

IgoR-NiK commented 1 year ago

DO 6.0.11

System.InvalidCastException when using Enum : short

using NUnit.Framework;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

var dc = new DomainConfiguration("sqlserver", "Data Source=.; Initial Catalog=EnumShortBug; Integrated Security=True;");

dc.UpgradeMode = DomainUpgradeMode.Recreate;

dc.Types.Register(typeof(TestEntity));

using (var domain = Domain.Build(dc))
using (var s = domain.OpenSession())
using (s.Activate())
using (var t = Session.Current.OpenTransaction())
{
    new TestEntity(1);

    // OK
    Assert.DoesNotThrow(
        () => Query.All<TestEntity>()
                    .Select(it => new { it.Id, EnumInt = EnumInt.Usd })
                    .GroupBy(it => it.EnumInt, it => new { it.Id, it.EnumInt })
                    .ToList());

    // FAIL
    Assert.DoesNotThrow(
        () => Query.All<TestEntity>()
                    .Select(it => new { it.Id, EnumShort = EnumShort.Usd })
                    .GroupBy(it => it.EnumShort, it => new { it.Id, it.EnumShort })
                    .ToList());

    t.Complete();
}

enum EnumInt
{
    Rub = 1,

    Usd = 2
}

enum EnumShort : short
{
    Rub = 1,

    Usd = 2
}

[HierarchyRoot]
class TestEntity : Entity
{
    public TestEntity(int id) : base(id)
    {
    }

    [Key]
    [Field]
    public int Id { get; set; }
}

Exception

System.InvalidCastException: "Unable to cast object of type 'System.Int32' to type 'System.Int16'."

at System.Data.SqlClient.SqlBuffer.get_Int16()
at System.Data.SqlClient.SqlDataReader.GetInt16(Int32 i)
at Xtensive.Sql.TypeMapper.ReadShort(DbDataReader reader, Int32 index)
at Xtensive.Sql.TypeMapping.ReadValue(DbDataReader reader, Int32 index)
at Xtensive.Orm.Providers.DbDataReaderAccessor.Read(DbDataReader source)
at Xtensive.Orm.Providers.Command.ReadTupleWith(DbDataReaderAccessor accessor)
at Xtensive.Orm.Providers.Command.<AsReaderOf>d__19.MoveNext()
at Xtensive.Orm.Providers.SqlSessionHandler.<Xtensive-Orm-Providers-IProviderExecutor-ExecuteTupleReader>d__51.MoveNext()
at Xtensive.Orm.Providers.SqlProvider.<OnEnumerate>d__12.MoveNext()
at Xtensive.Orm.Rse.Providers.ExecutableProvider.<GetEnumerator>d__12.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Xtensive.Orm.Rse.RecordSet.<GetGreedyEnumerator>d__12.MoveNext()
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at Xtensive.Core.EnumerableExtensions.<Batch>d__22`1.MoveNext()
at Xtensive.Core.EnumerableExtensions.<ApplyBeforeAndAfter>d__25`1.MoveNext()
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<>c.<<Main>$>b__0_1() in C:\EnumShortBug\EnumShortBug\Program.cs:line 27
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)