【发布时间】:2014-01-16 04:52:02
【问题描述】:
我有一个名为employee 的表,并且有几个列,如EmpID,FirstName,MiddleName,LastName,Address,EmailID,RegionID,DesgID,我正在使用 Dapper .Net 和扩展来处理 SQL Server 数据库。我使用 Dapper SqlExtensions 来插入、更新、删除功能,并使用 Dapper multimapper 选项来填充细节。所以我在上面的employee表中使用了下面的类
[Table("employee")] //To map the table to class
public class EmployeeModel
{
[Key] //Denote primary key
public Int32 EmpID { get; set; }
[Column("FirstName")] //Trying to map table column FirstName to variable First (Fails)
public string First { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string EmailID { get; set; }
public Int32 RegionID { get; set; }
public RegionModel Region { get; set; }
public DesignationModel Designation { get; set; }
public Int32 DesgID { get; set; }
public Int32 Add(EmployeeModel Details)
{
try
{
using (var connection = DBProviderFactory.GetOpenConnection()) //Creating IDbConnection Here
{
return Convert.ToInt32(connection.Insert(Details)); //Using Dapper Extension To Insert New Employee Details
}
}
catch (Exception ex)
{
return -1;
}
}
public Int32 Update(EmployeeModel Details)
{
try
{
using (var connection = DBProviderFactory.GetOpenConnection())
{
return Convert.ToInt32(connection.Update(Details)); //Using Dapper Extension to Update Employee Details
}
}
catch (Exception ex)
{
return -1;
}
}
public IEnumerable<EmployeeModel> AllEmployees()
{
try
{
using (var connection = DBProviderFactory.GetOpenConnection())
{
//Using multi mapper in Dapper to Fill All Employee Details such as region,state,country,designation details etc..
string query = @"select e.EmpID,e.FirstName,e.MiddleName,e.LastName,e.Address,e.EmailID
,r.RegionID as RID,r.Region,s.StateID,s.State,c.CountryID,c.Country,d.DesgID as DID,d.Designation
from employee as e inner join region as r on e.RegionID=r.RegionID
inner join state as s on r.StateID=s.StateID inner join country as c on
s.CountryID=c.CountryID inner join designation as d on e.DesgID=d.DesgID";
return connection.Query<EmployeeModel, RegionModel, StateModel, CountryModel,DesignationModel, EmployeeModel>(query,
(employee, region, state, country, designation) =>
{
employee.Region = region;
region.State = state;
state.Country = country;
employee.Designation = designation;
return employee;
}, splitOn: "RID,StateID,CountryID,DID");
}
}
catch (Exception ex)
{
return null;
}
}
}
public class EmployeeModelMapper : ClassMapper<EmployeeModel>
{
public EmployeeModelMapper()
{
Map(m => m.Region).Ignore();
Map(m => m.Designation).Ignore();
Map(m => m.First).Column("FirstName"); //Trying to map table column FirstName to variable First (Fails in the case of Multimapping)
AutoMap();
}
}
在上面的示例中,我尝试将表列 FirstName 映射到类变量 First 但在使用 Dapper connection.Query() 运行查询时失败,请参阅 AllEmployees() 中的 AllEmployees() 方法@类。
我尝试使用 Dapper Mapper 扩展的另一个选项,也可以在上面的代码中找到,参考 EmployeeModelMapper 类。
我的问题:
如何将所有表列映射到对应的类变量,以便在 Dapper 和扩展中通用。
【问题讨论】:
标签: c# .net sql-server asp.net-mvc-3 dapper