2017-08-27 118 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

抱歉說先生,但沒有工作,我堅持了幾個小時。請幫助我,如果你可以 –