【发布时间】:2012-04-19 07:39:54
【问题描述】:
比我的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 有超过 20 个条件(在 where 子句中),其中搜索参数可能为 null,所以我不想在所有查询中重复所有 where 语句......
【问题讨论】:
标签: c# sql-server orm dapper