的類映射是這樣的:
public sealed class ProductCategoryNavigationMap : ClassMap<ProductCategoryNavigation>
{
public ProductCategoryNavigationMap()
{
ReadOnly();
// Set "CategoryId" property as the ID column. Without this,
// OpenSession() threw an exception that the configuration was invalid
Id(x => x.CategoryId);
Map(x => x.CategoryNodeId);
Map(x => x.ParentCategoryNodeId);
Map(x => x.Name);
Map(x => x.Title);
Map(x => x.SeoUrl);
// The column name returned from the sproc is "VisibleInd",
// so this is here to map it to the "IsActive" property
Map(x => x.IsActive).Column("VisibleInd");
Map(x => x.DisplayOrder);
Map(x => x.ProductCount);
}
}
存儲過程的調用是這樣的:
public List<NavigationViewModel> GetNavigationViewModel(int portalId, int localeId)
{
const string sql = "EXEC [dbo].[Stored_Procedure_Name] @PortalId=:PortalId, @LocaleId=:LocaleId";
return _session.CreateSQLQuery(sql)
.AddEntity(typeof(ProductCategoryNavigation))
.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)
.List<ProductCategoryNavigation>()
.Select(x => new NavigationViewModel
{
CategoryId = x.CategoryId,
CategoryNodeId = x.CategoryNodeId,
ParentCategoryNodeId = x.ParentCategoryNodeId,
Name = x.Name,
Title = x.Title,
SeoUrl = x.SeoUrl,
IsActive = x.IsActive,
DisplayOrder = x.DisplayOrder,
ProductCount = x.ProductCount
})
.ToList();
}
的AddEntity呼籲說什麼實體類的結果映射,將使用上面定義的ProductCategoryNavigationMap:
.AddEntity(typeof(ProductCategoryNavigation))
如果你看上去很小心爲Y 「SQL」 變量的值,你會看到兩個參數:
- :PortalId
- :的LocaleID
那些通過撥打電話設置:
.SetInt32("PortalId", portalId)
.SetInt32("LocaleId", localeId)
然後致電.List<ProductCategoryNavigation>()
爲我們提供了一個IList,它允許我們使用LINQ來投射任何我們想要的東西。在這種情況下,我得到了一個NavigationViewModel列表,它與ProductCategoryNavigation目前相同,但可根據需要獨立於實體進行更改。
我希望這可以幫助其他開發者加入NHibernate!
感謝您的快速響應。我將發佈所有我已經工作的代碼,但直到8小時過去後,我纔回答自己的問題。 – 2011-12-23 22:52:29
教程鏈接中斷 – sweetfa 2012-10-14 10:22:58
@sweetfa謝謝。我刪除了鏈接,我正在尋找另一個鏈接。 – 2012-10-15 15:04:44