使用 Fluent NHibernate 设置类映射
continue
I'm new to using NHibernate and I struggled to find clear examples online of how to create a ClassMap for a stored procedure without using XML for the mappings. I recently got this working using the Fluent interfaces and wanted to share what I've learned.
The stored procedure in question returns an object like this:
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; }
}
So, how do I create a ClassMap that NHibernate will use to map the result of a stored procedure to this object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ClassMap 如下所示:
对存储过程的调用如下所示:
AddEntity 调用说明将结果映射到哪个实体类,它将使用上面定义的 ProductCategoryNavigationMap:
如果仔细查看“sql”变量的值,您将看到两个参数:
这些参数是通过调用来设置的:
然后调用
.List()
为我们提供了IList,它允许我们使用 LINQ 来投影我们想要的任何内容。在本例中,我得到了一个 NavigationViewModel 列表,它当前与 ProductCategoryNavigation 相同,但可以根据需要独立于实体进行更改。我希望这可以帮助其他刚接触 NHibernate 的开发人员!
The ClassMap looks like this:
The call to the stored procedure looks like this:
The AddEntity calls says what Entity class to map the results to, which will use the ProductCategoryNavigationMap defined above:
If you look carefully at the value of the "sql" variable, you'll see two parameters:
Those are set by making calls to:
Then the call to
.List<ProductCategoryNavigation>()
provides us with an IList, which allows us to use LINQ to project whatever we want. In this case I'm getting a List of NavigationViewModel, which is currently the same as ProductCategoryNavigation but can change independently of the entity as needed.I hope this helps other developers new to NHibernate!
假设您已正确安装 NHibernate,您将在存储类映射的位置创建一个新类。
创建一个类,如下所示:
设置完成后,您可以根据需要使用存储库。
请记住,这只是一个基本设置。数据库结构越复杂,类映射就越复杂。
Assuming you have NHibernate properly installed, you would create a new class where ever your are storing your class maps.
Create a class like:
Once you have that set up, you use your repositories as needed.
Keep in mind that is only a basic set up. The more complicated your database structure is, the more complicated your class map will get.