简体   繁体   English

Dapper.net 2.0无法抛出异常

[英]Dapper.net 2.0 Unable to cast exception

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.Func 2[System.Data.IDataReader,WebApplication1.Modal.Users]' to type 'System.Func 2[System.Data.IDataReader,System.Object]'. 无法将类型为'System.Func 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 MainSharedTable是

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 感谢任何帮助注意:我的数据库是MYSQL,如果没有强制转换返回的对象我收到一个字典

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: 获得上述异常的唯一方法是,当您使用不支持委托协同方差(3.5及更早版本)并运行如下代码片段的.NET Framework版本时:

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

which is simplified version of the Dapper.SqlMapper.GetTypeDeserializerImpl method. 这是Dapper.SqlMapper.GetTypeDeserializerImpl方法的简化版本。 The last line works on NET Framework 4.0 + and throws on NET Framework 3.5. 最后一行适用于.NET Framework 4.0 +并在.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). 要解决此问题,请在项目中定位.NET Framework 4.0或更高版本,或者安装适用于.NET Framework版本的Dapper软件包(如果存在)。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM