Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidCastException when using Enum : short #303

Open
IgoR-NiK opened this issue Feb 23, 2023 · 0 comments
Open

System.InvalidCastException when using Enum : short #303

IgoR-NiK opened this issue Feb 23, 2023 · 0 comments
Assignees

Comments

@IgoR-NiK
Copy link

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)
@alex-kulakov alex-kulakov self-assigned this Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants