Nbear存储过程映射


【IT168技术文档】

   存储过程映射的实体目前只支持手工编写,还不能从EntityGen生成。

    下面是连个典型的映射到存储过程Sales by Year的实体:

[StoreProcedure("Sales by Year")]
public interface SalesByYear : IEntity
{
DateTime ShippedDate { get; set; }
int OrderID { get; set; }
decimal Subtotal { get; set; }
string Year { get; set; }
}
[StoreProcedure("Sales by Year", FillByIndex=true)]
public interface SalesByYear2 : IEntity
{
DateTime ShippedDate { get; set; }
[ColumnName("OrderID")]
int SalesOrderID { get; set; }
decimal Subtotal { get; set; }
string Year { get; set; }
}

   

注意

    这两个实体的区别是,第二个实体的StoreProcedureAttribute的第二个参数指定了对返回的数据是按照索引填充到实体还是按照名称填充。按照索引填充可以忽略存储过程中返回的列的名称,但是,列的顺序和数量必须和实体定义中严格一致。这种填充方式到好处性能最好。

    不指定FillByIndex=true时,其默认值为false,表示按照返回的列的名称填充实体。按名称填充的好处是存储过程中的列的数量和顺序和实体定义中可以不一致,但是名称必须一致,只有名称对应的列能被正确填充。

    另外,这种类型的实体同样支持ColumnNameAttribute,可以映射属性到不同的列名的。

     强类型查询存储过程

     以上方式定义的实体可以通过Gateway.ExecuteProcedure<IEntityType>()系列方法进行查询,并且同样支持强类型查询语法和弱类型查询语法传递查询参数,并且支持存储过程输出参数。

     例如,下面定义了一个用于强类型查询以上的SalesByYear实体的查询辅助类:

   namespace _Entity
{
public class SalesByYear
{
public static PropertyItem ShippedDate = new PropertyItem("ShippedDate", "[",
"]", "@");
public static PropertyItem OrderID = new PropertyItem("OrderID", "[", "]", "@");
public static PropertyItem Subtotal = new PropertyItem("Subtotal", "[", "]", "@");
public static PropertyItem Year = new PropertyItem("Year", "[", "]", "@");
public static ProcedureParamItem Beginning_Date = new ProcedureParamItem ("Beginning_Date");
public static ProcedureParamItem Ending_Date = new ProcedureParamItem
("Ending_Date");
}
}

   

注意,加粗的最后两行代表两个参储过程的输入参数。可以使用下面的代码使用强类型查询语法对存储过
程进行查询,返回实体数组:

SalesByYear[] objs = gateway.ExecuteProcedure<SalesByYear>
(_Entity.SalesByYear.Beginning_Date == DateTime.Parse("1982-1-1") &
_Entity.SalesByYear.Ending_Date == DateTime.Parse("2000-1-1"));

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3