English 中文(简体)
Dapper 多重映射不以 NULLL 值拆分工作
原标题:Dapper MultiMap doesn t work with splitOn with NULL value
  • 时间:2012-05-24 20:21:49
  •  标签:
  • dapper

我对顶端的多个映射器试图在包含 < code> NULL 的列上拆分有问题。 Dapper 似乎不会即时转换对象, 我的映射功能接收到 < code> null 而不是对象 。

下面我的新测试:

    class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Category Category { get; set; }
    }
    class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
    public void TestMultiMapWithSplitWithNullValue()
    {
        var sql = @"select 1 as id,  abc  as name, NULL as description,  def  as name";
        var product = connection.Query<Product, Category, Product>(sql, (prod, cat) =>
        {
            prod.Category = cat;
            return prod;
        }, splitOn: "description").First();
        // assertions
        product.Id.IsEqualTo(1);
        product.Name.IsEqualTo("abc");
        product.Category.IsNotNull();
        product.Category.Id.IsEqualTo(0);
        product.Category.Name.IsEqualTo("def");
        product.Category.Description.IsNull();
    }

> 失败的线条是 product. isnotNull (); , 原因是 cat 传递到映射功能是 null .

我还在Assert类中添加了这个方法:

public static void IsNotNull(this object obj)
{
    if (obj == null)
    {
        throw new ApplicationException("Expected not null");
    }
}
最佳回答

这是"逐个设计" 虽然我可以重新审视一下

特别是,这种行为是为了帮助左翼加入。

cnn.Query<Car,Driver>("select * from Cars c left join Drivers on c.Id = CarId",
   (c,d) => {c.Driver = d; return c;}) 

问题是,如果我们允许创建 Driver 对象的“空白”, 每个 Car 都会有一个 Driver , 甚至加入失败时的“空白” 。

在绘制 < code> NULLL 对象之前,我们可以扫描被分割的整段,并确保所有值都为 < code> NULLL 。 这将对多个映像器产生非常小的 Perf 影响 。

为了解决你的案子,你可以插入一个替代栏目:

var sql = @"select 1 as id,  abc  as name,    as split, 
            NULL as description,  def  as name";
    var product = connection.Query<Product, Category, Product>(sql, (prod, cat) =>
    {
        prod.Category = cat;
        return prod;
    }, splitOn: "split").First();
问题回答

我也有同样的问题, 我被迫选择一个假的分裂柱 使达珀填充我的对象 而不是只是取消它;

我的工作方式:

    string req = @"
SELECT
    T1.a as PropA,
    T1.b as PropB,

    1 as Split,
    T2.a as PropA,
    T2.b as PropB,

    1 as Split,
    ...
FROM
    xxx T1,
    yyy T2,
    ...";
    using (var db = new OracleConnection(...))
    {
        return db.Query(
            req,
            (T1, T2) => {
                ...
            },
            splitOn: Split,Split,... );
    }

Dapper 应该有一个选项来避免 split on: split, split,...

对于所有想要可视化的人:

由最后相等的列名称拆分 :

将列的换换位置 :

无效问题 :

已交换列无效 :

分队营救:





相关问题
Does Dapper support the like operator?

Using Dapper-dot-net... The following yields no results in the data object: var data = conn.Query(@" select top 25 Term as Label, Type, ID from SearchTerms WHERE Term ...

Where to put sql when using dapper?

I m using dapper for a mvc3 project at work, and I like it. However, how are you supposed to layer the application when using dapper? Currently I just have all my sql stuffed directly in the ...

Is there a way to call a stored procedure with Dapper?

I am very impressed with the results of Dapper Micro ORM for stackoverflow.com. I am considering it for my new project and but I have one concern about that some times my project requires to have ...

Performing Inserts and Updates with Dapper

I am interested in using Dapper - but from what I can tell it only supports Query and Execute. I do not see that Dapper includes a way of Inserting and Updating objects. Given that our project (most ...

Dapper ORM paging and sorting

I am giving the Dapper ORM a try. I am able to query data from a table using the code below: Dim comments As List(Of Comment) Using conn = New SqlConnection(ConnectionString) conn.Open() ...

Map string to guid with Dapper

I m using Dapper to hammer out some load testing tools that need to access a PostgreSQL database. This particular version of PostgreSQL does not support GUIDs natively, so GUID values are stored as 32 ...

.NET SqlConnection and SqlCommand

I use SqlConnection and SqlCommand classes in my project with Dapper ORM but I ve got a strange problem. When I use SqlCommand for inserting a row in a db table it always work correct and when I ...

热门标签