NHibernate SessionFactory 和映射问题
之后,在互联网上阅读了许多小时的用于管理 NHibernate / Fluent Nhibernate 会话和配置的过时文档后,我实际上得到了一个无需使用 XML 即可工作的配置,我的 poco 和映射文件在 WebProject 中工作,我几乎感到兴奋。
然而;当我将返回 ISessionFactory 的帮助程序类移动到它应该位于的实际层时,没有任何效果,没有错误,我从会话工厂得到一个 ISession,但没有实体。请注意,我正在使用此类中的属性来恢复我的工厂,我知道这是不言自明的。 某种类型的错误如果在这里继续下去就好了。 代码:
public class NhibernateSessionFactoryHelper
{
private static ISessionFactory _sessionFactory;
private static string _connectionString =
ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISessionFactory BuildSessionFactory()
{
var cfg =
Fluently.Configure().ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName).Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Category>()).Database(MsSqlConfiguration.MsSql2008.ConnectionString(_connectionString)).Cache(c => c.UseQueryCache());
return cfg.BuildSessionFactory();
}
}
我已经删除了所有 Windsor 容器配置,以便更轻松地进行故障排除,因此我的基本设置如下。
网页界面 --->实体(category.cs) --->映射(categoryMap.cs) ---> FactoryGoo(NHibernateSessionFactory.cs
用于 POCO 的代码/
public class Category {
public Category() { }
public virtual int CategoryID { get; set; }
public virtual string CategoryName { get; set; }
public virtual string CategoryDescription { get; set; }
public virtual System.Nullable<int> ParentCategoryID { get; set; }
public virtual System.Nullable<int> DisplayOrder { get; set; }
public virtual int Active { get; set; }
public virtual string DateCreated { get; set; }
public virtual string LastUpdated { get; set; }
}
用于映射的实体代码
public class CategoryMap : ClassMap<Category> {
public CategoryMap() {
Table("Categories");
LazyLoad();
Id(x => x.CategoryID).GeneratedBy.Identity().Column("CategoryID");
Map(x => x.CategoryName).Column("CategoryName").Not.Nullable().Length(50);
Map(x => x.CategoryDescription).Column("CategoryDescription").Not.Nullable();
Map(x => x.ParentCategoryID).Column("ParentCategoryID");
Map(x => x.DisplayOrder).Column("DisplayOrder");
Map(x => x.Active).Column("Active").Not.Nullable();
Map(x => x.DateCreated).Column("DateCreated").Not.Nullable();
Map(x => x.LastUpdated).Column("LastUpdated").Not.Nullable();
}
}
因此,正如我之前所说,当所有类都位于同一个程序集中时,我会获取我的实体和数据。当我将 SessionFactory 移动到我的 SessionManagement 项目或映射到Infrastructure.Data.Mappings 项目和 Domain.Entities 项目的实体没有任何效果,并且我没有错误说明原因。
感谢您阅读本文,我希望我已经发布了足够的信息供您了解。设置的想法。
After, many hours of reading outdated documentation on the Internet for managing session and configuration of NHibernate / Fluent Nhibernate I actually got a configuration that works without using XML, my poco's and map files work in the WebProject and I almost got excited.
However; when I move helper class that returns ISessionFactory to the actual layer it should be in, nothing works, no errors, I get an ISession from the Session Factory just no Entites. Please note I am using the property in this class to get my Factory back, self explanatory I know.
Some sort of error would be great to go on here.
CODE:
public class NhibernateSessionFactoryHelper
{
private static ISessionFactory _sessionFactory;
private static string _connectionString =
ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISessionFactory BuildSessionFactory()
{
var cfg =
Fluently.Configure().ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName).Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Category>()).Database(MsSqlConfiguration.MsSql2008.ConnectionString(_connectionString)).Cache(c => c.UseQueryCache());
return cfg.BuildSessionFactory();
}
}
I have ripped out all my Windsor container config to make troubleshooting easier so I have the basic setup as follows.
Web.UI
---> entities (category.cs)
---> mappings (categoryMap.cs)
---> FactoryGoo (NHibernateSessionFactory.cs
CODE For POCO /Entity
public class Category {
public Category() { }
public virtual int CategoryID { get; set; }
public virtual string CategoryName { get; set; }
public virtual string CategoryDescription { get; set; }
public virtual System.Nullable<int> ParentCategoryID { get; set; }
public virtual System.Nullable<int> DisplayOrder { get; set; }
public virtual int Active { get; set; }
public virtual string DateCreated { get; set; }
public virtual string LastUpdated { get; set; }
}
Code for Mapping
public class CategoryMap : ClassMap<Category> {
public CategoryMap() {
Table("Categories");
LazyLoad();
Id(x => x.CategoryID).GeneratedBy.Identity().Column("CategoryID");
Map(x => x.CategoryName).Column("CategoryName").Not.Nullable().Length(50);
Map(x => x.CategoryDescription).Column("CategoryDescription").Not.Nullable();
Map(x => x.ParentCategoryID).Column("ParentCategoryID");
Map(x => x.DisplayOrder).Column("DisplayOrder");
Map(x => x.Active).Column("Active").Not.Nullable();
Map(x => x.DateCreated).Column("DateCreated").Not.Nullable();
Map(x => x.LastUpdated).Column("LastUpdated").Not.Nullable();
}
}
So as I stated earlier, I get my Entity and my data when all of classes live in the same assembly. When I move my SessionFactory to my SessionManagement project or the mapping to the Infrastructure.Data.Mappings project and the entities to the Domain.Entities project nothing works and I gt no error as to why.
Thanks for reading this, I hope I have posted enough for you to get an idea of the setup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保 BuildSessionFactory 方法中引用的类别类引用预期命名空间/项目中的类别类。也就是说,不同的项目中可能存在多个category类。
Make sure the category class referenced in the BuildSessionFactory method is referencing the category class in the expected namespace/project. In other words, there may be multiple category classes in different projects.
我当然不确定,但我认为您的意思是将父类别与您的类别链接起来,并且属于同一类型?如果是,则使用完整的类别对象作为属性而不是categoryId,并且不使用Map(),而是使用流畅的References()。
i'm not sure of course, but I think you meant to link the parent category with your category and that is of the same type? If yes, than use the full blown category object as a property instead of the categoryId, and not use Map() but use fluent References().