【问题标题】:Using Dapper.Net ORM, how do I cast stored procedure output to a concrete type?使用 Dapper.Net ORM,如何将存储过程输出转换为具体类型?
【发布时间】:2012-04-13 21:20:26
【问题描述】:

使用实体框架,我可以从我正在处理的项目的数据库中的大多数存储过程中创建具体的类。但是,一些存储过程使用动态 SQL,因此不会为存储过程返回元数据。

所以对于那个 sproc,我手动创建了一个具体的类,现在想将 sproc 输出映射到这个类并返回这个类型的列表。

使用下面的方法我可以得到一个对象的集合:

                var results = connection.Query<object>("get_buddies", 
                    new {   RecsPerPage = 100,
                            RecCount = 0,
                            PageNumber = 0,
                            OrderBy = "LastestLogin",
                            ProfileID = profileID,
                            ASC = 1}, 
                        commandType: CommandType.StoredProcedure);

我的具体类包含

[DataContractAttribute(IsReference=true)]
[Serializable()]
public partial class LoggedInMember : ComplexObject
{

   /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int16 RowID
    {
        get
        {
            return _RowID;
        }
        set
        {
            OnRowIDChanging(value);
            ReportPropertyChanging("RowID");
            _RowID = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("RowID");
            OnRowIDChanged();
        }
    }
    private global::System.Int16 _RowID;
    partial void OnRowIDChanging(global::System.Int16 value);
    partial void OnRowIDChanged();

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String NickName
    {
        get
        {
            return _NickName;
        }
        set
        {
            OnNickNameChanging(value);
            ReportPropertyChanging("NickName");
            _NickName = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("NickName");
            OnNickNameChanged();
        }
    }
    private global::System.String _NickName;
    partial void OnNickNameChanging(global::System.String value);
    partial void OnNickNameChanged();
    .
    .
    .

无需遍历结果并将输出参数添加到 LoggedInMember 对象,如何动态映射这些参数,以便通过 WCF 服务返回它们的列表?

如果我尝试var results = connection.Query&lt;LoggedInMember&gt;("sq_mobile_get_buddies_v35", ...,我会收到以下错误:

System.Data.DataException:解析列 0 时出错 (RowID=1 - Int64) ---> System.InvalidCastException:指定的强制转换无效。在反序列化...

【问题讨论】:

    标签: c#-4.0 stored-procedures sql-server-2008-r2 metadata dapper


    【解决方案1】:

    猜测您的 SQL 列是 bigint(即 Int64 又名 long),但您的 .Net 类型具有 n Int16 属性。

    您可以通过执行以下操作来玩转转换并忽略存储过程:

    var results = connection.Query<LoggedInMember>("select cast(9 as smallint) [RowID] ...");
    

    您只需选择要返回对象的属性和类型。 (smallintInt16 的 SQL 等价物)

    【讨论】:

      【解决方案2】:

      解决方案是使用 EF 创建一个从存储过程派生的复杂对象:

          public ProfileDetailsByID_Result GetAllProfileDetailsByID(int profileID)
          {
              using (IDbConnection connection = OpenConnection("PrimaryDBConnectionString"))
              {
                  try
                  {
                      var profile = connection.Query<ProfileDetailsByID_Result>("sproc_profile_get_by_id",
                          new { profileid = profileID },
                          commandType: CommandType.StoredProcedure).FirstOrDefault();
      
                      return profile;
                  }
                  catch (Exception ex)
                  {
                      ErrorLogging.Instance.Fatal(ex);        // use singleton for logging
                      return null;
                  }
              }
          }
      

      在这种情况下,ProfileDetailsByID_Result 是我通过复杂类型创建过程使用实体框架手动创建的对象(右键单击模型图,选择添加/复杂类型...,或使用复杂类型树上的右轴)。

      注意事项

      因为这个对象的属性是从存储过程中派生的,EF 无法知道一个属性是否可以为空。对于任何可为空的属性类型,您必须手动配置这些,方法是选择该属性并将它的 Nullable 属性设置为 true

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        相关资源
        最近更新 更多