所以我设置了一个带有一条记录的表的MySQL数据库。我的解决方案由三个项目组成(1个域模型库,测试库和我的Web项目)。在我的MVC项目中,我已经使用所有必需的Dll和
实现了NHibernate在Web项目根目录中:
nhibernate-configuration.xsd
nhibernate-mapping.xsd
nhibernate.config and
<classname>.hbm.xml file - with the class it is mapping
在我的Global.asax.cs文件中,我有我的事件处理程序来绑定当前会话: 公共类MvcApplication:System.Web.HttpApplication {
public MvcApplication()
{
BeginRequest += (MvcApplication_BeginRequest);
EndRequest += (MvcApplication_EndRequest);
}
void MvcApplication_BeginRequest(object sender, EventArgs e)
{
CurrentSessionContext.Bind(BootStrapper.SessionFactory.OpenSession());
}
void MvcApplication_EndRequest(object sender, EventArgs e)
{
CurrentSessionContext.Unbind(BootStrapper.SessionFactory).Dispose();
}
然后我有我的BootStrapper类,它返回当前会话: public static readonly ISessionFactory SessionFactory = CreateSessionFactory();
private static ISessionFactory CreateSessionFactory()
{
var cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
return cfg.BuildSessionFactory();
}
public static ISession GetSession()
{
return SessionFactory.GetCurrentSession();
}
我的控制器正由我的Ninject IoC传递一个对象 ProductController.cs 公共类ProductsController:控制器 { private readonly IProductsRepository productsRepository;
public ProductsController(IProductsRepository productsRepository)
{
this.productsRepository = productsRepository;
}
public ViewResult List()
{
return View(productsRepository.Products.ToList());
}
}
NinjectControllerFactory.cs public class NinjectControllerFactory:DefaultControllerFactory { //提供对象实例 private IKernel kernel = new StandardKernel(new DaisyblossomsServices());
//MVC calls this to get the controller for each requests
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (Controller)kernel.Get(controllerType);
}
}
您将销售哪些电话我的服务类DaisyblossomsServices: 公共类DaisyblossomsServices:NinjectModule {
public override void Load()
{
Bind<IProductsRepository>().To<ProductsRepository>();
}
}
你可以看到IProductsRepository绑定到我的ProductsRepository类:
public class ProductsRepository : IProductsRepository
{ 公共IQueryable产品 { get {var session = BootStrapper.GetSession();
return session.CreateCriteria(typeof(Product)).List<Product>().AsQueryable();
}
}
}
我的ProductsController交给了IProductsRepository对象
public interface IProductsRepository
{ IQueryable Products {get; } }
作为附加信息My Product.hbm.xml文件,它映射我的Product.cs类
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Daisyblossoms.Domain"
namespace="Daisyblossoms">
<class name="Product"
table="product">
<id name="ProductID">
<generator class="assigned" />
</id>
<property name="Name" column="Name" />
<property name="Price" column="Price" />
</class>
</hibernate-mapping>
我的nhibernate.config:
<?xml version="1.0"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Daisyblossoms.Domain">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="generate_statistics">true</property>
<property name="current_session_context_class">web</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<mapping assembly="Daisyblossoms.WebUI"/>
</session-factory>
</hibernate-configuration>
我的connectionsStrings是Web.config的一部分:
<connectionStrings>
<add name="daisyblossoms" connectionString="Server=localhost;Port=3306;Database=dbName;Uid=user;Pwd=somePSWD;pooling=false;"
providerName="MySql.Data.MySqlClient"/>
有什么想法可能是我的问题?
答案 0 :(得分:1)
验证hibernate.cfg.xml的输出设置为“Update if newer”,并确保* .hbm.xml文件标记为Embedded Resources。这是两个最常见的错误。听起来你也试图让很多活动部件同时工作。您可能希望简化操作以获得使用NHibernate连接到MySQL的控制台应用程序。像这样:
internal class Program {
private static void Main() {
var cfg = new Configuration();
cfg.Configure(); // Uses hibernate.cfg.xml by default.
// cfg.Configure("nhibernate.config"); // Or use this overload if you prefer your own name.
var sessionFactory = cfg.BuildSessionFactory();
using(var session = sessionFactory.OpenSession())
using(var tx = session.BeginTransaction()) {
var query = session.CreateCriteria<Product>().List();
query.ForEach(x => Console.WriteLine(x.Name));
tx.Commit();
}
Console.WriteLine("Press <ENTER> to exit...");
Console.ReadLine();
}
}
这将允许您验证您的映射和配置文件是否正确,而无需同时担心MVC,Ninject等。