Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/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
Class 使用Fluent NHibernate设置类映射_Class_Stored Procedures_Fluent Nhibernate_Map_Fluent Nhibernate Mapping - Fatal编程技术网

Class 使用Fluent NHibernate设置类映射

Class 使用Fluent NHibernate设置类映射,class,stored-procedures,fluent-nhibernate,map,fluent-nhibernate-mapping,Class,Stored Procedures,Fluent Nhibernate,Map,Fluent Nhibernate Mapping,我不熟悉使用NHibernate,我努力在网上找到清晰的示例,说明如何在不使用XML进行映射的情况下为存储过程创建类映射。我最近通过流畅的界面实现了这一点,并想与大家分享我学到的东西 所讨论的存储过程返回如下对象: public class ProductCategoryNavigation { public virtual int CategoryId { get; protected set; } public virtual int CategoryNodeId { get

我不熟悉使用NHibernate,我努力在网上找到清晰的示例,说明如何在不使用XML进行映射的情况下为存储过程创建类映射。我最近通过流畅的界面实现了这一点,并想与大家分享我学到的东西

所讨论的存储过程返回如下对象:

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; }
}
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();
}

那么,如何创建NHibernate将用来将存储过程的结果映射到此对象的类映射呢?

假设您已经正确安装了NHibernate,您将在存储类映射的位置创建一个新类

创建一个类,如:

public class PcnMap : ClassMap<ProductCategoryNavigation>
{
   Table("TableName");
   Id( x => x.CategoryId );
   Map( model => model.CategoryNodeId );
   // more like this for all your properties
}
公共类映射:类映射
{
表(“表名”);
Id(x=>x.CategoryId);
映射(model=>model.CategoryNodeId);
//对于您的所有属性,更像这样
}
一旦设置好了,就可以根据需要使用存储库


请记住,这只是一个基本设置。数据库结构越复杂,类映射就越复杂。

类映射如下所示:

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; }
}
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();
}
如果仔细查看“sql”变量的值,您将看到两个参数:

  • :手提
  • :LocaleId
  • 通过拨打以下电话进行设置:

    .SetInt32("PortalId", portalId)
    .SetInt32("LocaleId", localeId)
    
    然后调用
    .List()
    为我们提供了一个IList,它允许我们使用LINQ来投影我们想要的任何东西。在本例中,我将获得一个NavigationViewModel列表,它当前与ProductCategoryNavigation相同,但可以根据需要独立于实体进行更改


    我希望这能帮助其他新加入NHibernate的开发者

    谢谢你的快速回复。我将发布所有我正在使用的代码,但我不能回答我自己的问题,直到8小时过去。@sweetfa谢谢。我删除了链接,我正在寻找另一个链接。