有什么方法可以在 Fluent Nhibernate 中查询存储过程而不创建 hbm.xml 文件映射?
问问题
15019 次
3 回答
28
我假设您使用标准
Session.GetNamedQuery(....
相反,您可以使用
var result = Session.CreateSQLQuery("exec MyStoredProc :pUserId, :pIsLocked")
.AddEntity(typeof(MyDomainObject))
.SetParameter("pUserId", userId)
.SetParameter("pIsLocked", isLocked)
.List<MyDomainObject>();
这使您可以调用存储的过程,但仍然可以取回域对象(或列表),而无需 .hbm.xml 文件。
于 2013-07-15T09:02:58.073 回答
5
在我的情况下,您应该有一个返回结果集的类,它是 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();
于 2014-12-08T15:34:31.813 回答
1
这里有一些很好的答案,但是我想做一个更通用的解决方案,我可以传入我想要的对象并动态设置我的存储过程参数。
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);
于 2018-03-20T15:47:56.807 回答