Dapper.net 2.0无法抛出异常

时间:2017-09-15 13:02:52

标签: c# mysql asp.net dapper

接受的例外情况很明显。我以前确实使用过小巧玲珑而且效果很好,但现在很难过。

无法转换#System; System.Func 2[System.Data.IDataReader,WebApplication1.Modal.Users]' to type 'System.Func 2 [System.Data.IDataReader,System.Object]'。

类型的对象

用户类

public class Users : MainSharedTable
    {

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

    }

MainSharedTable是

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

并且异常发生在这里

public   IEnumerable<Users> GetAll()
    {

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

        return data;
    }

任何帮助表示赞赏 注意:我的数据库是MYSQL,如果不转换返回的对象,我会收到字典

全栈

[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

数据库中的表格如下enter image description here

周围没有什么奇怪的,我甚至把桌子大都化了,并使它们变小,但同样的错误就在那里

2 个答案:

答案 0 :(得分:2)

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

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

这是Dapper.SqlMapper.GetTypeDeserializerImpl方法的简化版本。最后一行适用于.NET Framework 4.0 +并在.NET Framework 3.5上抛出。

要解决此问题,请在项目中定位.NET Framework 4.0或更高版本,或者安装适合您的.NET Framework版本的Dapper软件包(如果存在)。

答案 1 :(得分:0)

您可以尝试更改功能

public   IEnumerable<Users> GetAll()
    {

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

        return data;
    }