2012-04-19 48 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上有一些可能相關的代碼。