Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NHibernate:从不在模型中的存储过程返回额外属性_Nhibernate_Nhibernate Mapping - Fatal编程技术网

NHibernate:从不在模型中的存储过程返回额外属性

NHibernate:从不在模型中的存储过程返回额外属性,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,在使用NHibernate执行存储过程时,是否有方法返回不在原始模型中的额外字段 我正在做的是创建一个存储过程来返回资源的搜索结果。它返回资源类的所有普通字段,但也返回一个名为Rank的额外字段。有没有办法将其映射到当前资源类(或者甚至创建一个从资源继承的类,只添加一个属性) 我的存储过程的执行是: <sql-query name="GetRelatedResources"> <return alias="item" class="ResourceRanked">

在使用NHibernate执行存储过程时,是否有方法返回不在原始模型中的额外字段

我正在做的是创建一个存储过程来返回资源的搜索结果。它返回资源类的所有普通字段,但也返回一个名为Rank的额外字段。有没有办法将其映射到当前资源类(或者甚至创建一个从资源继承的类,只添加一个属性)

我的存储过程的执行是:

<sql-query name="GetRelatedResources">
    <return alias="item" class="ResourceRanked">
        <return-property column="Rank" name="Rank" />
        <return-property column="ResourceId" name="Id" />
        <return-property column="Name" name="Name" />
        <return-property column="Description" name="Description" />
        <return-property column="Filename" name="Filename" />
        <return-property column="Filetype" name="Filetype" />
        <return-property column="IsPublic" name="IsPublic" />
        <return-property column="IsFeatured" name="IsFeatured" />
        <return-property column="VideoEmbedCode" name="VideoEmbedCode" />
        <return-property column="VideoId" name="VideoId" />
        <return-property column="VideoPlayerId" name="VideoPlayerId" />
        <return-property column="VideoPlayerKey" name="VideoPlayerKey" />
        <return-property column="VideoHeight" name="VideoHeight" />
        <return-property column="VideoWidth" name="VideoWidth" />
        <return-property column="IsDeleted" name="IsDeleted" />
        <return-property column="CreatedOn" name="CreatedOn" />
        <return-property column="ModifiedOn" name="ModifiedOn" />
        <return-property column="CreatedBy" name="CreatedBy" />
        <return-property column="ModifiedBy" name="ModifiedBy" />
        <return-property column="CreatedByName" name="CreatedByName" />
        <return-property column="ModifiedByName" name="ModifiedByName" />
    </return>
    exec dbo.gbi_sp_GetRelatedResources :pageSize, :pageIndex, :resourceId
</sql-query>

但是,除非复制资源类的xml映射,否则我无法实现这一点。我已经查看了nhibernate的子类,但它们似乎不适合我要做的事情。

我找不到任何适合我的解决方案,因此,不幸的是,我不得不专门为存储过程的返回结果映射另一个类。

更好的方法是在需要连接结果集的表上创建一个视图,并在存储过程中查询该视图,然后在.hbm文件中映射该视图。您可以在链接上查看详细信息

public class Resource : DomainEntity
{
    [Required(ErrorMessage = "Please enter a name"), StringLength(100, ErrorMessage = "Name length can not exceed 100 characters")]
    public virtual string Name { get; set; }

    [StringLength(200, ErrorMessage = "Description length can not exceed 200 characters")]
    public virtual string Description { get; set; }

    public virtual string Filename { get; set; }
    public virtual string Filetype { get; set; }

    public virtual bool IsPublic { get; set; }
    public virtual bool IsFeatured { get; set; }

    [StringLength(500, ErrorMessage = "Embed Code length can not exceed 500 characters")]
    public virtual string VideoEmbedCode { get; set; }
    public virtual long? VideoId { get; set; }
    public virtual long? VideoPlayerId { get; set; }
    [StringLength(100, ErrorMessage = "Player Key length can not exceed 100 characters")]
    public virtual string VideoPlayerKey { get; set; }
    public virtual int? VideoHeight { get; set; }
    public virtual int? VideoWidth { get; set; }

    //public virtual int Rank { get; set; }

    public virtual string Format { 
        get
        {
            if (ResourceFileType == ResourceFileType.EmbeddedVideo || ResourceFileType == ResourceFileType.VideoPlayer)
                return "Video";

            switch (Filetype)
            {
                case "pdf":
                    return "Adobe Acrobat";
                case "docx":
                case "doc":
                    return "Microsoft Word";
                case "ppt":
                case "pptx":
                    return "Microsoft PowerPoint";
                case "xls":
                case "xlsx":
                    return "Microsoft Excel";
                default:
                    return Filetype.ToUpper();
            }
        }
    }

    public virtual ResourceFileType ResourceFileType
    {
        get
        {
            if (VideoId.HasValue)
                return ResourceFileType.VideoPlayer;
            if (!VideoEmbedCode.IsNullOrEmpty() || VideoId.HasValue)
                return ResourceFileType.EmbeddedVideo;
            return ResourceFileType.Document;
        }
    }

    public virtual IEnumerable<Market> Markets { get; set; }
    public virtual IEnumerable<Workstream> Workstreams { get; set; }
    public virtual IEnumerable<Tag> Tags { get; set; }
    public virtual IEnumerable<Topic> Topics { get; set; }
    public virtual IEnumerable<ResourceType> Types { get; set; }

    public override string ToString()
    {
        return Id + " - " + Name;
    }
}
public class ResourceRanked : Resource
{
    public virtual int Rank { get; set; }
}