2017-08-27 79 views
1

我对NHibernate来说是全新的。我看到很多题目相同的问题,但我无法找到确切的错误。我使用NHibernate与SQL Server 2012的无法编译映射文档:NHibernate

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
     <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
     <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property> 
     <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=cafePOSdb;Integrated Security=True;</property> 
     <property name="show_sql">true</property> 
     <mapping assembly="CafePOS" /> 
    </session-factory> 
</hibernate-configuration> 

我的映射模型:

using System; 
using System.Text; 
using System.Collections.Generic; 

namespace CafePOS 
{ 
    public class CafeTableGroup 
    { 
     public CafeTableGroup() { } 
     public decimal cafe_table_group_id { get; set; } 
     public string cafe_table_group_name { get; set; } 
     public string remarks { get; set; } 
     public bool? is_active { get; set; } 
     public int? group_position { get; set; } 
     public string color { get; set; } 
    } 
} 

hbm.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping assembly="CafePOS" namespace="CafePOS" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="cafe_table_group" table="cafe_table_group" lazy="true" > 
    <id name="cafe_table_group_id" column="cafe_table_group_id"> 
     <generator class="identity" /> 
    </id> 
    <property name="cafe_table_group_name"> 
     <column name="cafe_table_group_name" sql-type="nvarchar" not-null="false" /> 
    </property> 
    <property name="remarks"> 
     <column name="remarks" sql-type="varchar" not-null="false" /> 
    </property> 
    <property name="is_active"> 
     <column name="is_active" sql-type="bit" not-null="false" /> 
    </property> 
    <property name="group_position"> 
     <column name="group_position" sql-type="int" not-null="false" /> 
    </property> 
    <property name="color"> 
     <column name="color" sql-type="nvarchar" not-null="false" /> 
    </property> 
    <bag name="cafe_table"> 
     <key column="cafe_table_group_id" /> 
     <one-to-many class="cafe_table" /> 
    </bag> 
    </class> 
</hibernate-mapping> 

我的SessionFactory类:

namespace CafePOS 
{ 
    public sealed class SessionFactory 
    { 
     private static volatile ISessionFactory iSessionFactory; 
     private static object syncRoot = new Object(); 
     public static ISession OpenSession 
     { 
      get 
      { 
       if (iSessionFactory == null) 
       { 
        lock (syncRoot) 
        { 
         if (iSessionFactory == null) 
         { 
          Configuration configuration = new Configuration(); 
         Assembly assembly = Assembly.GetCallingAssembly(); 
          configuration.AddAssembly(assembly); 
          iSessionFactory = configuration.BuildSessionFactory(); 
         } 
        } 
       } 
       return iSessionFactory.OpenSession(); 
      } 
     } 
    } 
} 

这是我想实现的功能:

public static string AddNewTableGroup(CafeTableGroup group) 
{ 
    using (ISession session = SessionFactory.OpenSession) 
    { 
     using (ITransaction transaction = session.BeginTransaction()) 
     { 
      try 
      { 
       session.Save(group); 
       transaction.Commit(); 
       return "1"; 
      } 
      catch (Exception ex) 
      { 
       transaction.Rollback(); 
       session.Close(); 
       throw ex.InnerException; 
      } 
     } 
    } 
} 

我上线

configuration.AddAssembly(assembly); 

显示在标题中错误错误:

未能进行编译映射文件:NHibernate

内部异常消息:

找不到方言在配置提前

感谢。

回答

1

你的类名是不是cafe_table_group,但CafeTableGroup

你可以通过改变你hbm.xml

<class name="CafeTableGroup" table="cafe_table_group" lazy="true"> 
      ************** 
0

您应该在根文件夹中有文件hibernate.cfg.xml。该文件的

样品内容是:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
     <property name="dialect">Netsis.Framework.Persister.Hibernate.Dialect.NMsSql2008Dialect, Netsis.Framework.Persister</property> 
     <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
     <property name="connection.connection_string">Data Source=localhost;Initial Catalog=CRM;Persist Security Info=True;User ID=user;Password=pass</property> 
     <property name="proxyfactory.factory_class">Netsis.Framework.Persister.Hibernate.Proxy.PropertyReaderProxyFactoryFactory,Netsis.Framework.Persister</property> 
     <property name="show_sql">True</property> 
     <property name="format_sql">True</property> 
     <property name="adonet.batch_size">30</property> 
    </session-factory> 
</hibernate-configuration> 
+0

先生尝试我使用Visual Studio 2015的hibernate.cfg.xml是在同一个位置App.Config中。 –

+0

好的 - 无法看到您的项目文件,所以我建议。我找到了另一个。发布另一个答案。感谢您能否确认,如果有效。 – HGMamaci

+0

抱歉说先生,但没有工作,我坚持了几个小时。请帮助我,如果你可以 –