C# 如何将多个SQL关系映射到c对象
我正在开发一个C WPF桌面应用程序,在这里我需要定期读/写SQL数据库SQL server。现在我想将数据库中的数据映射到C中的对象。我不能使用实体框架,所以我通过Dapper、slapper automapper和存储过程来访问所有数据 作为一个例子,我对这个示例数据库进行了建模 C对象看起来与此类似C# 如何将多个SQL关系映射到c对象,c#,sql,slapper.automapper,C#,Sql,Slapper.automapper,我正在开发一个C WPF桌面应用程序,在这里我需要定期读/写SQL数据库SQL server。现在我想将数据库中的数据映射到C中的对象。我不能使用实体框架,所以我通过Dapper、slapper automapper和存储过程来访问所有数据 作为一个例子,我对这个示例数据库进行了建模 C对象看起来与此类似 public class Manager { public string Name { get; set; } public string Phone { get; set; }
public class Manager {
public string Name { get; set; }
public string Phone { get; set; }
public List<Facility> Facilities {get; set;}
}
public class City {
public string Name { get; set; }
public string Description{ get; set; }
public List<Facility> Facilities {get; set;}
}
public class Facility {
public string Name { get; set; }
public string Description{ get; set; }
}
现在,我使用Slapper中的下划线符号Dapper从表中查询数据:
const string sql = @"SELECT
m.Name,
m.Phone,
m.ManagerId,
f.FacilityId As Facility_FacilityId,
f.WorkerCount As Facility_WorkerCount,
f.SquareFoot As Facility_SquareFoot,
f.Description As Facility_Description,
c.CityId AS Facility_City_CityId,
c.Name AS Facility_City_Name,
c.Description AS Facility_City_Description,
from Manager m
INNER JOIN dbo.Facility f ON m.ManagerId = f.ManagerId
INNER JOIN dbo.City c ON f.CityId = c.CityId";
using (System.Data.IDbConnection _connection = new System.Data.SqlClient.SqlConnection("Connstrint"))
{
var test = _connection.Query<dynamic>(sql);
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(Facility), new List<string> { "FacilityId" });
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(Manager), new List<string> { "ManagerId" });
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(City), new List<string> { "CityId" });
var testNetwork = (Slapper.AutoMapper.MapDynamic<Manager>(test) as IEnumerable<Manager>).ToList();
System.Diagnostics.Debugger.Break();
}
我知道贴图无法完全工作,因为设备无法进入对象中的城市。我应该画两张地图吗?一次从经理那里下来,然后从城市?还是我应该使用另一种设计模式