C# 如何在Dapper.Net中将数据库表列映射到类属性
我有一个名为C# 如何在Dapper.Net中将数据库表列映射到类属性,c#,.net,sql-server,asp.net-mvc-3,dapper,C#,.net,Sql Server,Asp.net Mvc 3,Dapper,我有一个名为employee的表,有几个列,如EmpID、FirstName、MiddleName、LastName、Address、EmailID、RegionID、DesgID,我正在使用Dapper.Net和扩展来处理SQL Server数据库。我使用Dapper SqlExtensions插入、更新、删除功能,并使用Dapper multimapper选项填充详细信息。因此,我在上面的employee表中使用了lower class [Table("employee")] //T
employee
的表,有几个列,如EmpID、FirstName、MiddleName、LastName、Address、EmailID、RegionID、DesgID
,我正在使用Dapper.Net和扩展来处理SQL Server数据库。我使用Dapper SqlExtensions插入、更新、删除功能,并使用Dapper multimapper选项填充详细信息。因此,我在上面的employee
表中使用了lower class
[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();
}
}
[Table(“employee”)]//将表映射到类
公共类雇员模型
{
[Key]//表示主键
公共Int32 EmpID{get;set;}
[Column(“FirstName”)]//尝试首先将表列FirstName映射到变量(失败)
第一个公共字符串{get;set;}
公共字符串MiddleName{get;set;}
公共字符串LastName{get;set;}
公共字符串地址{get;set;}
公共字符串EmailID{get;set;}
公共Int32 RegionID{get;set;}
公共区域模型区域{get;set;}
公共名称模型名称{get;set;}
public Int32 DesgID{get;set;}
公共Int32添加(员工模型详细信息)
{
尝试
{
使用(var connection=DBProviderFactory.GetOpenConnection())//在此处创建IDbConnection
{
返回Convert.ToInt32(connection.Insert(Details));//使用Dapper扩展插入新员工详细信息
}
}
捕获(例外情况除外)
{
返回-1;
}
}
公共Int32更新(EmployeeModel详细信息)
{
尝试
{
使用(var connection=DBProviderFactory.GetOpenConnection())
{
返回Convert.ToInt32(connection.Update(Details));//使用Dapper扩展更新员工详细信息
}
}
捕获(例外情况除外)
{
返回-1;
}
}
公共i可数所有员工()
{
尝试
{
使用(var connection=DBProviderFactory.GetOpenConnection())
{
//在Dapper中使用multi-mapper填写所有员工详细信息,如地区、州、国家、指定详细信息等。。
字符串查询=@“选择e.EmpID、e.FirstName、e.MiddleName、e.LastName、e.Address、e.EmailID
,r.RegionID为RID,r.Region为s.StateID,s.State为c.CountryID,c.Country为c.Country,d.DesgID为d
从雇员身份e内部加入区域身份r on e.RegionID=r.RegionID
内部连接状态为s on r.StateID=s.StateID内部连接国家/地区为c on
s、 CountryID=c.CountryID内部联接指定为e.DesgID=d.DesgID上的d”;
返回connection.Query(查询,
(员工、地区、州、国家、职务)=>
{
employee.Region=区域;
地区。州=州;
state.Country=国家;
雇员。指定=指定;
返回员工;
},splitOn:“RID,StateID,CountryID,DID”);
}
}
捕获(例外情况除外)
{
返回null;
}
}
}
公共类EmployeeModelMapper:ClassMapper
{
公共EmployeeModelMapper()
{
Map(m=>m.Region).Ignore();
Map(m=>m.Designation).Ignore();
Map(m=>m.First).Column(“FirstName”);//尝试首先将表列FirstName映射到变量(在多重映射的情况下失败)
自动映射();
}
}
在上面的示例中,我试图将表列FirstName
映射到类变量First
,但在使用Dapperconnection运行查询时失败。Query()
请参阅EmployeeModel
类中的AllEmployees()
方法
另外,我尝试使用Dapper Mapper扩展的另一个选项,也可以在上面的代码中找到,请参考EmployeeModelMapper
class
我的问题是:
如何将所有表列映射到它们相应的类变量,以便在Dapper和extensions中使用。一个简单的方法是将SQL select语句更改为使用
AS
,它将自动映射
因此,不是:
string query = @"select e.EmpID,e.FirstName, ....
你只需要
string query = @"select e.EmpID,e.FirstName as First, ....
一种简单的方法是将您的SQL select语句更改为使用
AS
,它将自动映射
因此,不是:
string query = @"select e.EmpID,e.FirstName, ....
你只需要
string query = @"select e.EmpID,e.FirstName as First, ....
默认情况下,Dapper不支持属性属性,例如
列
和键
这里有两个选项,调整查询以使用AS
关键字更改结果集中列的名称以匹配属性名称,这是最简单的选项
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using Dapper;
using Newtonsoft.Json;
using Repository.DTO;
namespace Repository.Repositories
{
public class EmployeeRepo
{
public IEnumerable<EmployeeModel> AllEmployees()
{
try
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Read"].ConnectionString))
{
const string query = @"select EmpID, FirstName [First] from employee";
return connection.Query<EmployeeModel>(query);
}
}
catch (Exception e)
{
Console.WriteLine(JsonConvert.SerializeObject(e));
throw;
}
}
}
}
下一步是初始化映射
using Dapper.FluentMap;
using Repository.DTO;
namespace Repository
{
public class Bootstrap
{
public static void Map()
{
FluentMapper.Initialize(config =>
{
config.AddMap(new EmployeeModel.EmployeeModelMap());
});
}
}
}
那就这样了
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using Dapper;
using Newtonsoft.Json;
using Repository.DTO;
namespace Repository.Repositories
{
public class EmployeeRepo
{
public IEnumerable<EmployeeModel> AllEmployees()
{
try
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Read"].ConnectionString))
{
const string query = @"select EmpID, FirstName from employee";
return connection.Query<EmployeeModel>(query);
}
}
catch (Exception e)
{
Console.WriteLine(JsonConvert.SerializeObject(e));
throw;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统配置;
使用System.Data.SqlClient;
使用整洁;
使用Newtonsoft.Json;
使用Repository.DTO;
名称空间存储库
{
公共类雇员
{
公共i可数所有员工()
{
尝试
{
使用(var connection=new SqlConnection(ConfigurationManager.ConnectionStrings[“Read”].ConnectionString))
{
const string query=@“从员工中选择EmpID和FirstName”;
返回连接。查询(Query);
}
}
捕获(例外e)
{
Console.WriteLine(JsonConvert.Seri