使用存储过程映射 Dapper.Net 中的导航属性
Map navigation properties in Dapper.Net using stored procedure
我正在使用 Dapper.Net 从 SQL 服务器数据库获取数据。
这是我的 POCO 类
public partial class Production
{
public System.Guid ProductionId { get; set; }
public System.Guid SurveyId { get; set; }
public Nullable<int> PercentComplete { get; set; }
public string CompletedBy { get; set; }
public string DeliverTo { get; set; }
public virtual SurveyJob SurveyJob { get; set; }
}
public partial class SurveyJob
{
public SurveyJob()
{
this.Productions = new HashSet<Production>();
}
public System.Guid SurveyId { get; set; }
public string JobTitle { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> JobNumber { get; set; }
public Nullable<System.DateTime> SurveyDate { get; set; }
public Nullable<System.DateTime> RequiredBy { get; set; }
public virtual ICollection<Production> Productions { get; set; }
}
我想获取所有作品及其 SurveyJob 信息。这是我在存储过程中的 SQL 查询,其中 returns 这些列
SELECT
P.ProductionId, S.SurveyId,
P.PercentComplete, P.CompletedBy, P.DeliverTo,
S.JobTitle, S.JobNumber, S.RequiredBy, S.Status, S.SurveyDate
FROM
dbo.Production P WITH(NOLOCK)
INNER JOIN
dbo.SurveyJob S WITH(NOLOCK) ON S.SurveyId = P.SurveyId
问题是我正在获取生产数据,但 SurveyJob 对象为空。
这是我的 C# 代码
var result = await Connection.QueryAsync<Production>("[dbo].[GetAllProductions]", p, commandType: CommandType.StoredProcedure);
我得到 SurveyJob
对象 null,如图所示。
需要帮助。我做错了什么?
你的模型对于你执行的查询是错误的,你的查询将 return 一个普通对象(dapper 总是 return 普通对象),所以你需要一个 class您正在选择的属性。
将您的模型更改为:
public partial class ProductionSurvey
{
public System.Guid ProductionId { get; set; }
public System.Guid SurveyId { get; set; }
public Nullable<int> PercentComplete { get; set; }
public string CompletedBy { get; set; }
public string DeliverTo { get; set; }
public System.Guid SurveyId { get; set; }
public string JobTitle { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> JobNumber { get; set; }
public Nullable<System.DateTime> SurveyDate { get; set; }
public Nullable<System.DateTime> RequiredBy { get; set; }
}
我正在使用 Dapper.Net 从 SQL 服务器数据库获取数据。
这是我的 POCO 类
public partial class Production
{
public System.Guid ProductionId { get; set; }
public System.Guid SurveyId { get; set; }
public Nullable<int> PercentComplete { get; set; }
public string CompletedBy { get; set; }
public string DeliverTo { get; set; }
public virtual SurveyJob SurveyJob { get; set; }
}
public partial class SurveyJob
{
public SurveyJob()
{
this.Productions = new HashSet<Production>();
}
public System.Guid SurveyId { get; set; }
public string JobTitle { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> JobNumber { get; set; }
public Nullable<System.DateTime> SurveyDate { get; set; }
public Nullable<System.DateTime> RequiredBy { get; set; }
public virtual ICollection<Production> Productions { get; set; }
}
我想获取所有作品及其 SurveyJob 信息。这是我在存储过程中的 SQL 查询,其中 returns 这些列
SELECT
P.ProductionId, S.SurveyId,
P.PercentComplete, P.CompletedBy, P.DeliverTo,
S.JobTitle, S.JobNumber, S.RequiredBy, S.Status, S.SurveyDate
FROM
dbo.Production P WITH(NOLOCK)
INNER JOIN
dbo.SurveyJob S WITH(NOLOCK) ON S.SurveyId = P.SurveyId
问题是我正在获取生产数据,但 SurveyJob 对象为空。
这是我的 C# 代码
var result = await Connection.QueryAsync<Production>("[dbo].[GetAllProductions]", p, commandType: CommandType.StoredProcedure);
我得到 SurveyJob
对象 null,如图所示。
需要帮助。我做错了什么?
你的模型对于你执行的查询是错误的,你的查询将 return 一个普通对象(dapper 总是 return 普通对象),所以你需要一个 class您正在选择的属性。
将您的模型更改为:
public partial class ProductionSurvey
{
public System.Guid ProductionId { get; set; }
public System.Guid SurveyId { get; set; }
public Nullable<int> PercentComplete { get; set; }
public string CompletedBy { get; set; }
public string DeliverTo { get; set; }
public System.Guid SurveyId { get; set; }
public string JobTitle { get; set; }
public Nullable<int> Status { get; set; }
public Nullable<int> JobNumber { get; set; }
public Nullable<System.DateTime> SurveyDate { get; set; }
public Nullable<System.DateTime> RequiredBy { get; set; }
}