2012-04-19 71 views
9

更高级一些的映射,然后在我的previous question :)小巧玲珑的中间映射

表:

create table [Primary] (
    Id int not null, 
    CustomerId int not null, 
    CustomerName varchar(60) not null, 
    Date datetime default getdate(), 
    constraint PK_Primary primary key (Id) 
) 

create table Secondary(
    PrimaryId int not null, 
    Id int not null, 
    Date datetime default getdate(), 
    constraint PK_Secondary primary key (PrimaryId, Id), 
    constraint FK_Secondary_Primary foreign key (PrimaryId) references [Primary] (Id) 
) 

create table Tertiary(
    PrimaryId int not null, 
    SecondaryId int not null, 
    Id int not null, 
    Date datetime default getdate(), 
    constraint PK_Tertiary primary key (PrimaryId, SecondaryId, Id), 
    constraint FK_Tertiary_Secondary foreign key (PrimaryId, SecondaryId) references Secondary (PrimaryId, Id) 
) 

类:

public class Primary 
{ 
    public int Id { get; set; } 
    public Customer Customer { get; set; } 
    public DateTime Date { get; set; } 
    public List<Secondary> Secondaries { get; set; } 
} 

public class Secondary 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
    public List<Tertiary> Tertiarys { get; set; } 
} 

public class Tertiary 
{ 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
} 

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

是否有可能使用一个选择,以填补他们所有?事情是这样的:

const string sqlStatement = @" 
    select 
     p.Id, p.CustomerId, p.CustomerName, p.Date, 
     s.Id, s.Date, 
     t.Id, t.Date 
    from 
     [Primary] p left join Secondary s on (p.Id = s.PrimaryId) 
     left join Tertiary t on (s.PrimaryId = t.PrimaryId and s.Id = t.SecondaryId) 
    order by 
     p.Id, s.Id, t.Id 
"; 

然后:

IEnumerable<Primary> primaries = connection.Query<Primary, Customer, Secondary, Tertiary, Primary>(
    sqlStatement, 
    ... here comes dragons ... 
    ); 

EDIT1 - 我可以有两个嵌套循环做到这一点(的foreach次级 - >的foreach第三会)进行,每一项目的查询,但只是想知道如果可以用单个数据库调用完成。

Edit2 - 也许QueryMultiple方法在这里是合适的,但如果我理解正确,那么我需要多个select语句。在我现实生活中的例子中,select有20多个条件(在where子句中),其中搜索参数可能为空,所以我不想重复所有查询中所有那些语句...

回答

4

小巧玲珑支持多映射,文档见:http://code.google.com/p/dapper-dot-net/

下面是从项目的一个范例之一,我目前的工作:

 var accounts2 = DbConnection.Query<Account, Branch, Application, Account>(
        "select Accounts.*, SplitAccount = '', Branches.*, SplitBranch = '', Applications.*" + 
        " from Accounts" + 
        " join Branches" + 
        "  on Accounts.BranchId = Branches.BranchId" + 
        " join Applications" + 
        "  on Accounts.ApplicationId = Applications.ApplicationId" + 
        " where Accounts.AccountId <> 0", 
        (account, branch, application) => 
        { 
         account.Branch = branch; 
         account.Application = application; 
         return account; 
        }, splitOn: "SplitAccount, SplitBranch" 
        ).AsQueryable(); 

诀窍是使用splitOn选项,将记录集分成多个对象。

您还可以查看我的问题,以查看上例的班级结构:Dapper Multi-mapping Issue

1

看起来像在所有的ORM你都会有几个查询。您只能创建自己的解决方案,可能基于Dapper或Petapoco。例如,将所有查询在一个SQL批处理:

select * from Primary where ... 
select * from Secondary where ... 
select * from Tertiary where ... 

然后,你可以从一个记录导航使用DataReader.NextResult()

的话,需要在内存中完成的对象结构的数据结合起来,NEX 。

1

如何创建SQLCommand,然后创建一堆SQLParameter对象。理想情况下使用存储过程但不一定是。

然后可以将这些输出参数中的每一个映射回您的类。

This other post Stack上有一些可能相关的代码。