之后,在因特网上阅读过时的文档来管理NHibernate/Fluent Nhibernate的会话和配置,我实际上得到了一个不使用XML的配置,我的poco和map文件在WebProject中工作,我几乎感到兴奋。NHibernate SessionFactory和映射问题
但是;当我移动返回ISessionFactory的帮助器类到它应该在的实际层时,没有任何工作,没有错误,我从Session Factory获得一个ISession,只是没有Entites。请注意我正在使用这个类的属性来让我的工厂回来,我自我解释我知道。 某种错误在这里会很棒。 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();
}
}
我已经撕去了我所有的温莎容器的配置,使故障排除更容易,所以我有基本的设置如下。
Web.UI --->实体(category.cs) --->映射(categoryMap.cs) ---> FactoryGoo(NHibernateSessionFactory.cs
CODE对于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项目时,什么都不起作用,我不知道为什么。
感谢您阅读本文,我希望我已经发布足够的内容让您了解该设置。