NHibernate提示表is not mapped

兵工廠三劍客發表於2018-07-13

一定要將hbm.xml對映檔案設定成嵌入的資源。

下面是mysql的對映檔案:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name=" wh.db.test.model.Test, wh.db.test.model" table="test">
    <!--主鍵-->
    <id name="id" column="id" type="string" unsaved-value="0">
      <!--非自增主鍵應該配置為assigned屬性,自增主鍵應該配置成native-->
      <generator class="assigned" />
    </id>
    <property name="name" column="name" type="string"/>
    <property name="sex" column="sex" type="int"/>
    <property name="address" column="address" type="string"/>
    <property name="work" column="work" type="string"/>
  </class>
</hibernate-mapping>

下面是oracle的對映檔案:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="EPAP.SZ.Models.B_R_Szmjobcar, EPAP.SZ.Models" table="B_R_SZMJOBCAR">
    <id name="Jobcarid" column="JOBCARID" type="int">
      <generator class="sequence">
        <param name="sequence">B_R_SZMJOBCAR_SEQ</param>
      </generator>
    </id>
    <!--系統標識-->
    <property name="Epapid" column="EPAPID" type="string" />
    <!--作業分工單ID-->
    <property name="Jdlistid" column="JDLISTID" type="int" />
    <!--汽車管理ID-->
    <property name="Carinfoid" column="CARINFOID" type="int" />
    <!--車號-->
    <property name="Carno" column="CARNO" type="string" />
    <!--汽車司機-->
    <property name="Driver" column="DRIVER" type="string" />
    <!--汽車司機ID-->
    <property name="Driverid" column="DRIVERID" type="string" />
    <!--汽車司機電話-->
    <property name="Drivertel" column="DRIVERTEL" type="string" />
    <!--乘客-->
    <property name="Passengers" column="PASSENGERS" type="string" />
    <!--乘客ID-->
    <property name="Passengersid" column="PASSENGERSID" type="string" />
    <!--編號-->
    <property name="Serialnumber" column="SERIALNUMBER" type="int" />
    <!--作業前送地點-->
    <property name="Sendaddress" column="SENDADDRESS" type="string" />
    <!--作業後接地點-->
    <property name="Receiveaddress" column="RECEIVEADDRESS" type="string" />
    <!--出車時間-->
    <property name="Dispatchtime" column="DISPATCHTIME" type="DateTime" />
    <!--司機攜帶工具id-->
    <property name="DriverCarryToolId" column="DRIVERCARRYTOOLID" type="int" />
   
  </class>
</hibernate-mapping>

mysql的web.config配置參考:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    <!--用Log4Net記錄NHibernate中執行的SQL語句及執行時間-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <!--NHibernate配置-->
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <!-- 版本改為 5-->
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <!--使用什麼資料庫-->
      <property name="connection.connection_string">Server=127.0.0.1;Database=whtest;User ID=root;Password=123456</property>
      <!-- Data Source=FirstSample.sdf  改為Server= localhost;Database=mygamedb;user ID=root;Password=root -->
      <property name="show_sql">true</property>
      <!--不配置hbm2ddl會報列“ReservedWord”不屬於表 ReservedWords的錯誤-->
      <property name="hbm2ddl.keywords">none</property>
      <!--載入對映-->
      <mapping assembly="wh.db.test.model"/>

    </session-factory>
  </hibernate-configuration>
  <!--log4net配置-->
  <log4net debug="true">
    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="D:\\Services\\Logs\\Error\\" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMM\\yyyy-MM-dd".log"" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <!--只接受最小等級與最大等級之間的日誌輸出,其他的被過濾,這裡接受WARN(警告),ERROR(錯誤),FATAL(致命)級別的輸出-->
        <param name="LevelMin" value="WARN" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ErrorLog" />
    </root>
  </log4net>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--需要配置該節點,不然HttpContext.current是為空的-->
    <httpModules>
      <add name="NHSessionFactory" type="wh.db.test.dao.DataBase.NHSessionFactory" />
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 為避免洩漏後設資料資訊,請在部署前將以下值設定為 false 並刪除上面的後設資料終結點 -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障異常詳細資訊以進行除錯,請將以下值設定為 true。在部署前設定為 false 以避免洩漏異常資訊 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--必須配置aspNetCompatibilityEnabled為true否則HttpContext.current為空-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

oracle的web.config參考:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    <!--用Log4Net記錄NHibernate中執行的SQL語句及執行時間-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings file="Config/EPAPConfig.config">
    <add key="Epapid" value="025" />
    <add key ="WebUrl" value="http://192.168.1.161:1125"/>
  </appSettings>
  <!--nhibernate配置-->
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="ora10gFactory">
      <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
      <!--<property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>-->
      <property name="connection.connection_string">
        <!--User Id=epapsz;Password=epapsz;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=TMIS)))-->
      </property>
      <property name="adonet.batch_size">10</property>
      <property name="show_sql">false</property>
      <property name="command_timeout">60</property>
      <!--載入hibernate自動更新資料庫結構,update為更新,none為不更新-->
      <property name="hbm2ddl.auto">none</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
      <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property>
      <property name="hbm2ddl.keywords">none</property>
      <property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>
      <property name="cache.use_second_level_cache">false</property>
      <property name="cache.use_query_cache">false</property>

      <!--載入對映-->
      <mapping assembly="EPAP.SZ.Models" />
      <mapping assembly="EPAP.SZ.ViewModels" />
    </session-factory>
  </hibernate-configuration>
  <!--log4net配置-->
  <log4net debug="true">
    <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="D:\\Services\\Logs\\Error\\" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyyMM\\yyyy-MM-dd".log"" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <!--只接受最小等級與最大等級之間的日誌輸出,其他的被過濾,這裡接受WARN(警告),ERROR(錯誤),FATAL(致命)級別的輸出-->
        <param name="LevelMin" value="WARN" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ErrorLog" />
    </root>
  </log4net>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpModules>
      <add name="NHSessionFactory" type="EPAP.SZ.DAL.DataBase.NHSessionFactory" />
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 為避免洩漏後設資料資訊,請在部署前將以下值設定為 false 並刪除上面的後設資料終結點 -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- 要接收故障異常詳細資訊以進行除錯,請將以下值設定為 true。在部署前設定為 false 以避免洩漏異常資訊 -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--管理會話狀態-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
    <!--考勤一體機服務-->
    <bindings>
      <basicHttpBinding>
        <!--其實要修改所有的服務,不管是服務端還是客戶端,Binding那邊增加一個沒有設定名字的預設配置就OK了 maxBufferSize="2147483647"-->
        <binding   closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
        maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
        </binding >
      </basicHttpBinding>
    </bindings>
    <!--考勤一體機服務end-->

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
</configuration>

相關文章