3

The exception that am recieving is clear. i did use dapper before and it worked great but now am having hard time.


Unable to cast object of type 'System.Func2[System.Data.IDataReader,WebApplication1.Modal.Users]' to type 'System.Func2[System.Data.IDataReader,System.Object]'.

User Class is

public class Users : MainSharedTable
    {

        public int FkReceiverID { get; set; }
        public string Password { get; set; }

    }

MainSharedTable is

public class MainSharedTable 
    {
        public int ID { get; set; }
        public bool Active { get; set; }
    }

and the exception happens here

public   IEnumerable<Users> GetAll()
    {

            var conn = GetOpenConnection();
           var data = conn.Query<Users>("SELECT * FROM arabaicsms.users  "); // error happens here 
            ConnectionClose();

        return data;
    }

any help is appreciated note: my db is MYSQL and if do not cast the returned object i receive a dictionary

full stack

[InvalidCastException: Unable to cast object of type 'System.Func`2[System.Data.IDataReader,WebApplication1.Modal.Users]' to type 'System.Func`2[System.Data.IDataReader,System.Object]'.]
   Dapper.SqlMapper.GetTypeDeserializerImpl(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) +10454
   Dapper.TypeDeserializerCache.GetReader(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) +412
   Dapper.TypeDeserializerCache.GetReader(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) +379
   Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) +72
   Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) +520
   Dapper.<QueryImpl>d__58`1.MoveNext() +1293
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +387
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +119
   Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) +787
   WebApplication1.BL.UsersBs.GetAll() in C:\Users\pc\documents\visual studio 2015\Projects\ArabaicSmsWeb\WebApplication1\BL\UsersBs.cs:25
   WebApplication1.login.Page_Load(Object sender, EventArgs e) in C:\Users\pc\documents\visual studio 2015\Projects\ArabaicSmsWeb\WebApplication1\login.aspx.cs:17
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +780

the table at the database is like below enter image description here

there is nothing strange around, i did even capitalize the table latter and make them small also but the same error is there

MuhanadY
  • 752
  • 1
  • 12
  • 40
  • i did add the stack trace on my last edit. thanks – MuhanadY Sep 15 '17 at 13:16
  • can you please try by merging MainSharedTable class with Users class and then recheck again – Kevin Shah Sep 15 '17 at 13:25
  • @KevinShah i did so but still have the same exception – MuhanadY Sep 15 '17 at 13:27
  • can you put your table schema here, just want to check does it match with the class properties or not – Kevin Shah Sep 15 '17 at 14:13
  • @KevinShah sorry for being late to answer but i was out of the office. i did update the question again with database image it may help. i did not find any thing in that ! – MuhanadY Sep 18 '17 at 07:19
  • can you please try by changing public bool Active { get; set; } to public short Active { get; set; } – Kevin Shah Sep 18 '17 at 08:41
  • @KevinShah nop... nothing change. by the way i do not think that its a type specific casting error. mostly the hole object it self. when i retrieve data from mysql with the mysql command and i receive no error. beside that when i use no cast on dapper the date received as dic. and no error too – MuhanadY Sep 18 '17 at 09:02
  • @marcgravell any idea. your help is appreciated – MuhanadY Sep 18 '17 at 09:21
  • @MuhanadY try to specify the `Names` rather than `*` in your select – Krsna Kishore Sep 18 '17 at 10:41
  • @Webruster i have done that too in the select statement. Farther more i retrieved just one property ID which is int, and nothing changed – MuhanadY Sep 18 '17 at 10:44
  • @MuhanadY try to select only two fields that is `FKRecievedID` and `Password` in your select and paste if you get error – Krsna Kishore Sep 18 '17 at 10:46
  • Are you sure that WebApplication1.Modal.Users is class and not a struct? – Evk Sep 18 '17 at 12:26
  • 1
    What you do mean by **2.0** in *Dapper.net 2.0*? There is no such Dapper version (the latest is 1.5.2). The only way you can get such exception with the posted class is if you target some NET Framework which does not support delegate co/contra variance (any before 4.0). – Ivan Stoev Sep 18 '17 at 12:49
  • @IvanStoev yes your right and i did find the right ver. from nuget now!!! the system started to work again like a charm. please right your answer as new post to make it as an answer – MuhanadY Sep 18 '17 at 13:40

2 Answers2

2

The only way you can get the aforementioned exception is when you target NET Framework version which does not support delegate co/contra variance (3.5 and earlier) and running a code snippet like this:

Delegate funcA = new Func<string, string>(s => s);
var funcB = (Func<string, object>)funcA;

which is simplified version of the Dapper.SqlMapper.GetTypeDeserializerImpl method. The last line works on NET Framework 4.0 + and throws on NET Framework 3.5.

To fix the issue, either target NET Framework 4.0 or later in your project, or install the Dapper package appropriate for your NET Framework version (if one exists).

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • 1
    Thank for your support.the true problem was because of the setting dapper.net download. Thank you – MuhanadY Sep 18 '17 at 15:47
0

Can you try by changing your function

public   IEnumerable<Users> GetAll()
    {

            var conn = GetOpenConnection();
           IEnumerable<Users> data = conn.Query<Users>("SELECT * FROM arabaicsms.users  "); // error happens here 
            ConnectionClose();

        return data;
    }
Kevin Shah
  • 1,589
  • 1
  • 13
  • 20
  • 1
    brother that does nothing changing from var to IEnumerable. but to give it a shot i did so and nothing new – MuhanadY Sep 18 '17 at 09:26
  • Can you please try by passing command type with dapper query like commandType: CommandType.TableDirect or commandType: CommandType.Text – Kevin Shah Sep 18 '17 at 09:32
  • tanks. commandtype.text make nothing beside that tabledirect is just for ole db .net and wont work with mysql – MuhanadY Sep 18 '17 at 09:39