2011-02-02 20 views
0

我試圖映射以下結構:由(select top 1 HistoryDate from History where ContentID = {tagid} && Type = 'Activated' order by HistoryDate desc不知道了如何使用功能NHibernate從自定義查詢的對象上的屬性

我實際上是填充

public class Tag { 
    public Guid Id {get;set;} 
    public DateTime ActivatedDate {get;set;} 
} 

public class History { 
    public Guid Id {get;set;} 
    public TypeEnum Type {get;set;} 
    public Guid ContentID {get;set;} 
    public DateTime HistoryDate {get;set;} 
} 

Tag.ActivatedDate真的不知道如何用Fluent NHibernate映射這個。

我的映射是:

public TagMapping() { 
    Table("Tags"); 
    Id(x => x.Id)  
} 

public HistoryMapping() { 
    Table("History"); 
    Id(x => x.Id); 
    Map(x => x.Type).CustomeType<TypeEnum>(); 
    Map(x => x.ContentID); 
    Map(x => x.HistoryDate); 
} 

我不知道如何映射Tag.ActivatedTop

基本上在尋找:

SELECT tag.Id, 
     (select top 1 HistoryDate from History 
       where ContentID = tag.Id 
       AND Status = 'Activated' 
     order by HistoryDate desc) As ActivatedDate 
FROM Tags tag 
+0

你的意思是ActivatedDate在「我是n確定如何映射Tag.ActivatedTop「? – brainimus 2011-02-02 21:57:42

回答

0

落得這樣做:

Map(x => x.ActivatedDate) 
       .ReadOnly() 
       .Formula(
        string.Format(
         "(select top 1 h.HistoryDate from History h where h.ContentID = Id AND h.Status = 'Activated' order by h.HistoryDate desc)")); 
+0

完成此操作後,我決定做一些反規範化處理,並將ActivatedDate列添加到我的標記表中。 – Rob 2011-02-09 16:30:44

相關問題