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;


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

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

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


enum EnumInt
    Rub = 1,

    Usd = 2

enum EnumShort : short
    Rub = 1,

    Usd = 2

class TestEntity : Entity
    public TestEntity(int id) : base(id)

    public int Id { get; set; }


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)

asked Feb 16 at 14:20

Igor%20Nikolaev's gravatar image

Igor Nikolaev

edited Feb 16 at 14:28

Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA