将Dapper
库与Sql Server
一起使用。是否可以使用Predicates
和Complex Sql Queries
传递必需的参数,以便将Sql Resultset过滤为Single Entity Class
?
SELECT * FROM Config.Country
INNER JOIN Config.State
ON Config.State.CountryId = Config.Country.CountryId
实体类可以具有以下结构。
public sealed class CountryStateReadDto
{
//Country table
public byte CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public string ISOCode { get; set; }
public string DailingCode { get; set; }
public string WebCode { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
//State table
public short StateId { get; set; }
public string StateName { get; set; }
}
我知道,有一个漂亮的Dapper-Extension项目可以完成这项工作,但仅适用于Single Table only
。
我最好使用Complex Sql Queries
来编写Predicates
。有人可以通过提供一些提示来帮助我,甚至解决方案也会非常有用!
如果Predicate
不是一个理想的解决方案,那么还有什么我可以考虑或优于Predicate
吗?
注意:上面的Sql示例只是一个简单的查询,我有复杂的 使用>查询10个表连接。
答案 0 :(得分:1)
我建议创建一个视图
Create View StateDetails
SELECT * FROM Config.Country
INNER JOIN Config.State
ON Config.State.CountryId = Config.Country.CountryId
设置视图后,使用Dapper变得更加容易。