0
[Table("department_types", Schema = "public")]
public class DepartmentTypes
[Table("departments", Schema = "public")]
public class Department
查詢:
private string BuildQuery()
{
return OrmConfiguration.GetSqlBuilder<DepartmentViewModel>().Format(
[email protected]" SELECT {nameof(Department.Id):C},
{nameof(Department.Name):C},
{nameof(Department.TypeId):C},
{nameof(DepartmentTypes.Name):C} as DepartmentTypeName
FROM {nameof(Department):T}
LEFT JOIN {nameof(DepartmentTypes):T}
ON {nameof(Department.TypeId):C} = {nameof(DepartmentTypes.Id):C}");
}
結果類:
public class DepartmentViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string DepartmentTypeName { get; set; }
}
我想和部和DepartmentTypes聯接,並得到一個DepartmentViewModel結果...
現在,而不是{nameof(DepartmentTypes):T}
(對於實例mple)系統放入「DepartmentViewModel」,而不是DepartmentTypes的名稱,即:「department_types」
如果它會更容易,我試了一下你的方式,然後只是做了我創建我自己的字符串插入原始SQL查詢。只要你有適合你的列的表格標識符,它仍然可以工作。此外,由於您聲明瞭作爲您的實體,因此您可以使用:T和:C項目來縮短某些表/列聲明。 –