The exception that am recieving is clear. i did use dapper before and it worked great but now am having hard time.
2[System.Data.IDataReader,WebApplication1.Modal.Users]' to type 'System.Func
2[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
there is nothing strange around, i did even capitalize the table latter and make them small also but the same error is there
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).
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;
}