【问题标题】:Inserting an IEnumerable<T> collection with Dapper errors out with "class is not supported by Dapper."插入带有 Dapper 错误的 IEnumerable<T> 集合,并带有“Dapper 不支持类”。
【发布时间】:2011-09-17 06:58:38
【问题描述】:

是的,有 questions herehere 关于如何使用 dapper-dot-net 插入记录。然而,这些答案虽然内容丰富,但似乎并没有为我指明正确的方向。情况如下:将数据从 SqlServer 移动到 MySql。将记录读入IEnumerable&lt;WTUser&gt; 很容易,但我只是在插入时没有得到任何东西。一、'移动记录代码':

//  moving data
Dim session As New Session(DataProvider.MSSql, "server", _
                           "database")

Dim resources As List(Of WTUser) = session.QueryReader(Of WTUser)("select * from tbl_resource")


session = New Session(DataProvider.MySql, "server", "database", _
                      "user", "p@$$w0rd")

//    *edit* - corrected parameter notation with '@'
Dim strInsert = "INSERT INTO tbl_resource (ResourceName, ResourceRate, ResourceTypeID, ActiveYN) " & _
                "VALUES (@ResourceName, @ResourceRate, @ResourceType, @ActiveYN)"

Dim recordCount = session.WriteData(Of WTUser)(strInsert, resources)

//  session Methods
    Public Function QueryReader(Of TEntity As {Class, New})(ByVal Command As String) _
                                                            As IEnumerable(Of TEntity)
        Dim list As IEnumerable(Of TEntity)

        Dim cnn As IDbConnection = dataAgent.NewConnection
        list = cnn.Query(Of TEntity)(Command, Nothing, Nothing, True, 0, CommandType.Text).ToList()

        Return list
    End Function

    Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, ByVal Entities As IEnumerable(Of TEntity)) _
                                                          As Integer
        Dim cnn As IDbConnection = dataAgent.NewConnection

        //    *edit* if I do this I get the correct properties, but no data inserted
        //Return cnn.Execute(Command, New TEntity(), Nothing, 15, CommandType.Text)

        //    original Return statement
        Return cnn.Execute(Command, Entities, Nothing, 15, CommandType.Text)
    End Function

cnn.Query 和 cnn.Execute 调用 dapper 扩展方法。现在,WTUser 类(注意:列名从 SqlServer 中的“WindowsName”更改为 MySql 中的“ResourceName”,因此两个属性指向同一个字段):

Public Class WTUser
    //    edited for brevity - assume the following all have public get/set methods
    Public ActiveYN As String
    Public ResourceID As Integer
    Public ResourceRate As Integer
    Public ResourceType As Integer
    Public WindowsName As String
    Public ResourceName As String

End Class

我收到来自 dapper 的异常:“Dapper 不支持 WTUser。” DataMapper(dapper)中的这个方法:

    private static Action<IDbCommand, object> CreateParamInfoGenerator(Type OwnerType)
    {
        string dmName = string.Format("ParamInfo{0}", Guid.NewGuid());
        Type[] objTypes = new[] { typeof(IDbCommand), typeof(object) };

        var dm = new DynamicMethod(dmName, null, objTypes, OwnerType, true); // << - here
        //    emit stuff

        //    dm is instanced, now ...
        foreach (var prop in OwnerType.GetProperties().OrderBy(p => p.Name))

此时 OwnerType =

System.Collections.Generic.List`1[[CRMBackEnd.WTUser, CRMBE,版本=1.0.0.0, 文化=中性, PublicKeyToken=null]], mscorlib, 版本=2.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089

看来 OwnerType 应该是 CRMBackEnd.WTUser ... 而不是 List&lt;CRMBackEnd.WTUser&gt; ... ???因为正在发生的事情是正在迭代集合属性:计数、容量等。我错过了什么?

更新

如果我将 session.WriteData 修改为:

Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, _
                                                      ByVal Entities As IEnumerable(Of TEntity)) _
                                                      As Integer
    Dim cnn As IDbConnection = dataAgent.NewConnection
    Dim records As Integer

    For Each entity As TEntity In Entities
        records += cnn.Execute(Command, entity, Nothing, 15, CommandType.Text)
    Next

    Return records
End Function

记录被很好地插入......但我认为这不是必要的,例如:

connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  ).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"  

...来自dapper-dot-net

【问题讨论】:

    标签: c# vb.net insert dapper


    【解决方案1】:

    我刚刚为此添加了一个测试:

    class Student
    {
        public string Name {get; set;}
        public int Age { get; set; }
    }
    
    public void TestExecuteMultipleCommandStrongType()
    {
        connection.Execute("create table #t(Name nvarchar(max), Age int)");
        int tally = connection.Execute(@"insert #t (Name,Age) values(@Name, @Age)", new List<Student> 
        {
            new Student{Age = 1, Name = "sam"},
            new Student{Age = 2, Name = "bob"}
        });
        int sum = connection.Query<int>("select sum(Age) from #t drop table #t").First();
        tally.IsEqualTo(2);
        sum.IsEqualTo(3);
    }
    

    它像宣传的那样工作。我对 multi-exec 的工作方式进行了一些修改(所以它更快一点并且支持 object[])。

    我的猜测是您遇到了问题,因为您在 WTUser 上的所有字段上都缺少 getter 属性。所有参数都必须具有读取器属性,我们不支持从字段中提取它,它需要一个复杂的解析步骤才能保持效率。


    导致问题的另一点是向 dapper 传递一个带有不受支持的映射的参数。

    例如,不支持以下类作为参数:

    class Test
    {
       public int Id { get; set; }
       public User User {get; set;}
    }
    
    cnn.Query("select * from Tests where Id = @Id", new Test{Id = 1}); // used to go boom 
    

    问题是 dapper 没有解析 SQL,它假定所有的 props 都可以设置为参数,但无法解析 User 的 SQL 类型。

    最新版本解决了这个问题

    【讨论】:

    • 已编辑 WTUser:所有 Public 字段 都是 getter/setter ...为简洁起见,我进行了编辑:VB 非常冗长。我会澄清这个帖子。 ;)
    • @Sam 这个解决方案是通用的吗?即它可以在 Oracle 上运行吗?
    • 如何取回我插入的实体的 ID?
    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多