【发布时间】:2017-01-27 20:37:23
【问题描述】:
我有两张桌子: 用户和用户类型:
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[UserTypeId] [int] NOT NULL
)
CREATE TABLE [dbo].[UserType](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL
)
我的模型类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public UserType UserType { get; set; }
}
public class UserType
{
public int Id { get; set; }
public string Name { get; set; }
}
我的查询:
SELECT
U.Id
, U.Name
, UT.Id AS [UserTypeId]
, UT.Name AS [UserTypeName]
FROM dbo.User AS F
INNER JOIN dbo.UserType AS UT ON U.UserTypeId = UT.Id
ORDER BY U.Id
还有我的映射器类:
public class UserMapper : CrudEntityMapper<User>
{
public UserMapper() : base("User")
{
Property(x => x.UserType)
.ColumnName("UserTypeId")
.ToPropertyValue((x) => new UserType { Id = (int)x });
Property(x => x.UserType)
.ColumnName("UserTypeName")
.ToPropertyValue((x) => new UserType { Name = (string)x });
}
}
当我尝试执行命令时,我得到没有 userType.Id 的用户列表(Id 始终 = 0)。我需要填写我的User 和子UserType 类的数据。
请告诉我我做错了什么。
cmd.ToList<User>();
PS。我使用Griffin.Framework 进行映射
【问题讨论】:
标签: c# sql-server mapper