3

我是使用NHibernate的新手,我努力在網上找到關於如何爲存儲過程創建一個ClassMap而無需使用XML映射的清晰示例。我最近使用Fluent界面得到了這個工作,並想分享我學到的東西。用Fluent NHibernate進行班級地圖設置

有問題的存儲過程返回這樣一個對象:

public class ProductCategoryNavigation 
{ 
    public virtual int CategoryId { get; protected set; } 
    public virtual int CategoryNodeId { get; set; } 
    public virtual int ParentCategoryNodeId { get; set; } 
    public virtual string Name { get; set; } 
    public virtual string Title { get; set; } 
    public virtual string SeoUrl { get; set; } 
    public virtual bool IsActive { get; set; } 
    public virtual int DisplayOrder { get; set; } 
    public virtual int ProductCount { get; set; } 
} 

所以,我怎麼創建的NHibernate將使用映射存儲過程到該對象的結果的類映射?

回答

0

假設你已經正確安裝了NHibernate,你可以創建一個新的類來存儲你的類地圖。

創建一個類,如:

public class PcnMap : ClassMap<ProductCategoryNavigation> 
{ 
    Table("TableName"); 
    Id(x => x.CategoryId); 
    Map(model => model.CategoryNodeId); 
    // more like this for all your properties 
} 

一旦你的設置,你用你的資料庫需要。

請記住,這只是一個基本的設置。你的數據庫結構越複雜,你的班級地圖就會越複雜。

+0

感謝您的快速響應。我將發佈所有我已經工作的代碼,但直到8小時過去後,我纔回答自己的問題。 – 2011-12-23 22:52:29

+0

教程鏈接中斷 – sweetfa 2012-10-14 10:22:58

+0

@sweetfa謝謝。我刪除了鏈接,我正在尋找另一個鏈接。 – 2012-10-15 15:04:44

8

的類映射是這樣的:

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」 變量的值,你會看到兩個參數:

  1. :PortalId
  2. :的LocaleID

那些通過撥打電話設置:

.SetInt32("PortalId", portalId) 
.SetInt32("LocaleId", localeId) 

然後致電.List<ProductCategoryNavigation>()爲我們提供了一個IList,它允許我們使用LINQ來投射任何我們想要的東西。在這種情況下,我得到了一個N​​avigationViewModel列表,它與ProductCategoryNavigation目前相同,但可根據需要獨立於實體進行更改。

我希望這可以幫助其他開發者加入NHibernate!

+0

AddEntity方法是純金,謝謝 – rjlopes 2017-07-17 12:11:54