-- =============================================
-- Author: Lyndonliu
-- =============================================
ALTER PROCEDURE [dbo].[SP_DemoGetUserInfo]
AS
BEGIN
SELECT
UserInfo.Name AS '姓名',
UserInfo.Age AS '年齡'
FROM UserInfo
END
namespace CustomColumn2ndLibrary.Models
{
///
/// User Info
///
public class User
{
///
/// User Name
///
public string Name { get; set; }
///
/// User Age
///
public int Age { get; set; }
}
}
using Dapper.FluentMap.Mapping;
namespace CustomColumn2ndLibrary.Models
{
public class UserMap : EntityMap< User>
{
public UserMap()
{
// ToColumn() 方法中
// caseSensitive 參數設為 false,可以呼略欄位名稱的大小寫
Map(x => x.Name).ToColumn("姓名");
Map(x => x.Age).ToColumn("年齡");
}
}
}
Dapper 查詢
List< User> lUser = null;
using (SqlConnection con = new SqlConnection(Info.LyndonliuConnectionString))
{
// 初始化 Map
FluentMapper.Initialize(cfg =>
{
cfg.AddMap(new UserMap());
});
// 執行 Query
lUser = con.Query< User>("SP_DemoGetUserInfo",
commandType: CommandType.StoredProcedure,
commandTimeout: 0).ToList();
}
Ref:
- https://dotblogs.com.tw/supershowwei/2016/08/16/175753
沒有留言:
張貼留言