有什么办法来查询功能NHibernate的存储过程,而无需创建一个hbm.xml文件中映射?
Answer 1:
我假设你使用标准
Session.GetNamedQuery(....
相反,你可以使用
var result = Session.CreateSQLQuery("exec MyStoredProc :pUserId, :pIsLocked")
.AddEntity(typeof(MyDomainObject))
.SetParameter("pUserId", userId)
.SetParameter("pIsLocked", isLocked)
.List<MyDomainObject>();
这使您可以调用存储过程,但仍然得到一个域对象(或清单),而不需要的.hbm.xml文件。
实际上,有这个帖子
Answer 2:
你应该在我的情况下返回的结果集一类是GameActivity类
public class GameActivity
{
public virtual DateTime Date { get; set; }
public virtual string GameRoundId { get; set; }
public virtual int GameProvider { get; set; }
public virtual string GameName { get; set; }
public virtual decimal RealBet { get; set; }
public virtual decimal RealWin { get; set; }
public virtual decimal BonusBet { get; set; }
public virtual decimal BonusWin { get; set; }
public virtual decimal BonusContribution { get; set; }
public virtual int IsRoundCompleted { get; set; }
public virtual int IsRoundCancelled { get; set; }
}
调用存储过程“GetMemberGameActivity”以获取列表
var result = session.CreateSQLQuery("exec GetMemberGameActivity :mToken, :StartDate, :EndDate")
.SetResultTransformer(Transformers.AliasToBean())
.SetParameter("mToken", token)
.SetParameter("StartDate", startDate)
.SetParameter("EndDate", endDate)
.List().ToList();
Answer 3:
这里一些很好的答案,但是我想做出更通用的解决方案,我可以在我想出来的对象传递,并动态地设置我的存储过程的参数。
public RequestList<T> FetchExport<T>(Integration_ExportType exportType)
{
var returnRequest = new RequestList<T>{Success = true};
try
{
string sql = "EXEC "+exportType.StoredProcedure+" :@" + string.Join(", :@",exportType.Parameters.GetType().GetProperties().Select(pinfo => pinfo.Name).ToArray());
var session = Session.CreateSQLQuery(sql).SetResultTransformer(Transformers.AliasToBean<T>());
foreach (var parameter in exportType.Parameters.GetType().GetProperties())
{
session.SetParameter("@" + parameter.Name, parameter.GetValue(exportType.Parameters,null));
}
returnRequest.Obj = session.List<T>().ToList();
return returnRequest;
}
catch (Exception exception )
{
returnRequest.Success = false;
returnRequest.Message = exception.Message;
returnRequest.Exception = exception;
return returnRequest;
}
}
一般类型的一个例子 - 映射到存储过程的输出和创建用于被连接到一个专利对象的存储过程的参数的未映射的对象。
public class Integration_ExportRERW_Scores
{
public string SchoolId { get; set; }
public string SSID { get; set; }
public int LessonId { get; set; }
public int ClassPeriod { get; set; }
public string TeacherId { get; set; }
public string LessonTitle { get; set; }
public int RW4ComprehensionScore { get; set; }
public int RW4WordPowerScore { get; set; }
public int REComprehensionScore { get; set; }
public int REVocabularyScore { get; set; }
public int RE2ComprehensionScore { get; set; }
public int RE2VocabularyScore { get; set; }
}
public class Integration_ExportRERW_Scores_Parameters
{
public int DistrictId { get; set; }
}
它是最后如何实现
var export = service.ListSubscriptions().First().Export;
var parameters = new Integration_ExportRERW_Scores_Parameters
{
DistrictId = 12060
};
export.Parameters = parameters;
var fetch = service.ExportData<Integration_ExportRERW_Scores>(export);
文章来源: Fluent NHibernate to query stored procedure without an hbm.xml mapping