Fluent NHibernate to query stored procedure without an hbm.xml mapping

后端 未结 3 2005
感动是毒
感动是毒 2020-12-24 13:36

Is there any way to query stored procedure in Fluent Nhibernate without creating an hbm.xml file mapping?

3条回答
  •  离开以前
    2020-12-24 14:23

    some good answers here however I want to make a more generic solution where I can pass in the object I want out and dynamically set my stored procedure parameters.

     public RequestList FetchExport(Integration_ExportType exportType)
         {
             var returnRequest = new RequestList{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());
    
                 foreach (var parameter in exportType.Parameters.GetType().GetProperties())
                 {
                     session.SetParameter("@" + parameter.Name, parameter.GetValue(exportType.Parameters,null));
                 }
                returnRequest.Obj = session.List().ToList();
    
                 return returnRequest;
             }
             catch (Exception exception )
             {
                 returnRequest.Success = false;
                 returnRequest.Message = exception.Message;
                 returnRequest.Exception = exception;
                 return returnRequest;
    
             }
         }
    

    An example of a generic type - mapped to the output of the stored procedure & create an un-mapped object for the stored procedure parameters that gets attached to a patent object.

    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; }
    }
    

    Finally how it's implemented

     var export = service.ListSubscriptions().First().Export;
             var parameters = new Integration_ExportRERW_Scores_Parameters
             {
                 DistrictId = 12060
             };
             export.Parameters = parameters;
             var fetch = service.ExportData(export);
    

提交回复
热议问题