精致小巧简单的映射 [英] Dapper simple mapping

查看:110
本文介绍了精致小巧简单的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表:

create table Documents 
   (Id int, 
    SomeText varchar(100), 
    CustomerId int, 
    CustomerName varchar(100)
   )

insert into Documents (Id, SomeText, CustomerId, CustomerName) 
   select 1, '1', 1, 'Name1' 
     union all
   select 2, '2', 2, 'Name2'

类:

public class Document
{
    public int Id { get; set; }
    public string SomeText { get; set; }
    public Customer { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}



我怎样才能得到所有文档客户与精致小巧的?这给我的所有文件,但客户为空(当然):

How can I get all Documents with their Customers with Dapper? This gives me all documents, but the customer is null (of course):

connection.Query<Document>("select Id, SomeText, CustomerId, CustomerName from Documents")...

编辑 - 相似,但更先进的制图问题:

EDIT - similar, but more advanced mapping question: Dapper intermediate mapping

推荐答案

项目页面(见多Mapping部分)

Example taken from dapper project page (see the Multi Mapping section):

var sql = 
@"select * from #Posts p 
left join #Users u on u.Id = p.OwnerId 
Order by p.Id";

var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();

post.Content.IsEqualTo("Sams Post1");
post.Id.IsEqualTo(1);
post.Owner.Name.IsEqualTo("Sam");
post.Owner.Id.IsEqualTo(99);

这篇关于精致小巧简单的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆