2

使用 Entity Framework,我可以从我正在处理的项目的数据库中的大多数存储过程中创建具体的类。但是,一些存储过程使用动态 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<LoggedInMember>("sq_mobile_get_buddies_v35", ...,我会收到以下错误:

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

4

2 回答 2

1

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

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

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

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

于 2012-06-05T21:23:19.720 回答
0

The solution to this was to create a complex object derived from the sproc with 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;
            }
        }
    }

In this case, ProfileDetailsByID_Result is the object that I manually created using Entity Framework through the Complex Type creation process (right-click on the model diagram, select Add/Complex Type..., or use the Complex Types tree on the RHS).

A WORD OF CAUTION

Because this object's properties are derived from the sproc, EF has no way of knowing if a property is nullable. For any nullable property types, you must manually configure these by selecting the property and setting its it's Nullable property to true.

于 2012-06-06T14:07:19.380 回答