C# 短小的中间映射

C# 短小的中间映射,c#,sql-server,orm,dapper,C#,Sql Server,Orm,Dapper,略高于my:)中的映射 表: 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

略高于my:)中的映射

表:

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; }
}
然后:

IEnumerable<Primary> primaries = connection.Query<Primary, Customer, Secondary, Tertiary, Primary>(
    sqlStatement,
    ... here comes dragons ...
    );
IEnumerable primaries=connection.Query(
sqlStatement,
……龙来了。。。
);
Edit1-我可以用两个嵌套循环(foreach secondaries->foreach tertiaries)完成它,并对每个项执行查询,但我想知道是否可以用单个数据库调用完成


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

似乎在所有ORM中,您将有多个查询。您只能创建自己的解决方案,可能基于Dapper或Petapoco。例如,将所有查询合并到一个SQL批处理中:

select * from Primary where ...
select * from Secondary where ...
select * from Tertiary where ...
然后,您可以使用DataReader.NextResult()从一个记录集导航到nex


然后,需要组合内存中的数据以完成对象结构。

创建一个SQLCommand,然后创建一组SQLParameter对象怎么样。理想情况下使用存储的进程,但不必如此

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


堆栈上有一些可能相关的代码。

Dapper支持多重映射,有关文档,请参阅:

以下是我目前正在进行的一个项目的一个示例:

        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();
var accounts2=DbConnection.Query

        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();