我正在尝试在一个项目中实现 Insight.Database,并且在尝试利用自动接口实现并将对象属性映射到数据库中的奇数列名时遇到了障碍。
我有以下结构......
class Employee
{
string EmployeeCode {get; set;}
string Name {get; set;}
}
class Vacation
{
int VacationId {get; set;}
DateTime VacationDate {get; set;}
string EmployeeCode {get; set;}
string ApprovedByEmployeeCode {get; set;}
Employee Employee {get; set;}
Employee ApprovedByEmployee {get; set;}
}
我的数据库看起来像......
表:员工(员工代码,[姓名])
表:假期(VacationId、VacationDate、EmployeeCode、ApprovedByEmployeeCode)
视图:VacationsView(所以我不必一遍又一遍地编写[和更改]相同的 SELECT)
SELECT v.VacationId, v.VacationDate, v.EmployeeCode, v.ApprovedByEmployeeCode, e1.EmployeeCode AS CreatedByEmployeeCode, e1.[Name] AS CreatedByName, e2.EmployeeCode AS ApprovingEmployeeCode, e2.[Name] AS ApprovingName
FROM Vacations v
INNER JOIN Employees e1 ON v.EmployeeCode = e1.EmployeeCode
INNER JOIN Employees e2 ON v.ApprovedByEmployeeCode = e2.EmployeeCode
存储过程:GetAllVacations
SELECT * FROM VacationsView
最后,使用 Insight.Database,我试图拥有一个接口,该接口将自动填充我的对象并告诉它如何将存储过程中的不同列名用于“员工”属性。
public interface IMyRepository
{
IList<Vacation> GetAllVacations();
}
....
var repo = conn.As<IMyRepository>();
return repo.GetAllVacations();
这有效(因为没有错误)并且我的假期的所有属性都已正确映射,但我的两个“员工”属性为空(正如预期的那样,因为列名不符合员工对象的属性名)。我想不通的是如何告诉 Insight“使用 CreatedBy ..”字段来构建“员工”属性和“审批...”字段来构建“ApprovedByEmployee”属性。
我已经能够使用带有回调和 columnOverride 的 OneToOne 来完成它,然后使用标准 Query()。IE。
var vacationStructure =
new OneToOne<Vacation, Employee, Employee>(
callback: (vacation, createdBy, approvedBy) =>
{
vacation.Employee = createdBy;
vacation.ApprovedByEmployee = approvedBy;
}, columnOverride: new ColumnOverride[]
{
new ColumnOverride<EmployeeModel>("CreatedByEmployeeCode", "EmployeeCode"),
new ColumnOverride<EmployeeModel>("CreatedByName", "Name"),
new ColumnOverride<EmployeeModel>("ApprovingEmployeeCode", "EmployeeCode"),
new ColumnOverride<EmployeeModel>("ApprovingName", "Name")
});
....
var results = await conn.QueryAsync("GetAllVacations", new {employeeCode}, Query.Returns(_vacationStructure));
但是,我真的在尝试利用 Insight 的自动界面功能。
我正在尝试做的事情可能吗?