有沒有一種方法在使用NHibernate執行存儲過程時返回一個不在原始模型中的額外字段?NHibernate:從存儲過程中返回的額外屬性不在模型中
我正在做的是創建一個sproc來返回資源的搜索結果。它會返回我的Resource類的所有正常字段,但也會返回一個名爲Rank的額外字段。有沒有辦法將它映射到當前的Resource類(或者甚至創建一個從Resource繼承的類並只添加了一個屬性)?
我的存儲過程的執行是:
<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>
而我的等級:
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; }
}
但我不能得到這個工作,除非我複製資源類的XML映射。我已經看過nhibernate的子類,但它們似乎不適合我想要做的事情。