有没有办法在不创建hbm.xml文件映射的情况下在Fluent Nhibernate中查询存储过程?
答案 0 :(得分:27)
我假设您使用标准
Session.GetNamedQuery(....
相反,您可以使用
var result = Session.CreateSQLQuery("exec MyStoredProc :pUserId, :pIsLocked")
.AddEntity(typeof(MyDomainObject))
.SetParameter("pUserId", userId)
.SetParameter("pIsLocked", isLocked)
.List<MyDomainObject>();
这允许您调用存储过程,但仍然可以获取域对象(或列表)而无需.hbm.xml文件。
答案 1 :(得分:4)
在我的情况下,你应该有一个返回结果集的类,它是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();
答案 2 :(得分: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;
}
}
泛型类型的一个示例 - 映射到存储过程的输出&amp;为附加到专利对象的存储过程参数创建一个未映射的对象。
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);