2010-12-21 51 views
0

嗯,我一直在争取这一点,现在我似乎无法找到一个应该很简单的解决方案。我得到了一个类(非常简单的类),我得到了hbm.xml(SET AS EMBEDDED RESOURCE)。我为SQLite设置了配置文件,但仍然得到[Class]未映射。nHibernate和SQLite = [Class]没有映射

这里就是错误出现,当我运行此:我中心类中

Public Sub LoadCentersFromDatabase() 
     Try 
      Dim session As ISession = OpenSession() 
      Dim query As IQuery = session.CreateQuery("from Center") 
      Dim foundCenters As IList(Of Center) = query.List(Of Center)() 
      MsgBox(foundCenters.Count) 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 

代码

Public Class Center 


#Region " Class Constructors " 

    Protected Sub New() 

    End Sub 

    Public Sub New(ByVal centerName As String, ByVal address As String, ByVal city As String, ByVal state As String, ByVal zip As String, ByVal country As String, ByVal phone As String) 
     Me.Id = 0 
     Me.ExternalId = -1 
     Me.CenterName = centerName 
     Me.Address = address 
     Me.City = city 
     Me.State = state 
     Me.ZIP = zip 
     Me.Country = country 
     Me.Phone = phone 
    End Sub 

    Public Sub New(ByVal id As Integer, ByVal centerName As String, ByVal address As String, ByVal city As String, ByVal state As String, ByVal zip As String, ByVal country As String, ByVal phone As String) 
     Me.New(centerName, address, city, state, zip, country, phone) 
     Me.Id = id 
    End Sub 

#End Region 

#Region " Declared Auto Properties " 

    Public Property Id As Integer 
    Public Property ExternalId As String 
    Public Property CenterName As String 
    Public Property Address As String 
    Public Property City As String 
    Public Property State As String 
    Public Property ZIP As String 
    Public Property Country As String 
    Public Property Phone As String 

#End Region 


End Class 

这是你hml.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="KC.Domain.Center, KC" table="Centers" lazy="true"> 
    <id name="Id" column="CenterId"> 
     <generator class="native" /> 
    </id> 
    <property name="CenterName" unique="true" /> 
    <property name="ExternalId" /> 
    <property name="Address" /> 
    <property name="City" /> 
    <property name="State" /> 
    <property name="ZIP" /> 
    <property name="Country" /> 
    <property name="Phone" /> 
    </class> 
</hibernate-mapping> 
代码

该App.Config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
<configSections> 
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> 
</configSections> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
<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.SQLite20Driver, NHibernate</property> 
    <property name="connection.connection_string"> 
     Data Source=C:\Users\Public\Documents\cats.db;Version=3 
    </property> 
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property> 
    <property name="query.substitutions">true=1;false=0</property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> 
    <mapping assembly="KC.Domain"/> 
    </session-factory> 
</hibernate-configuration> 
</configuration> 

回答

1

根据您的映射文件,Center类存在于KC程序集中。根据您的配置,该程序集被命名为KC.Domain。验证包含Center类的程序集的名称。您的映射类可能需要说:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="KC.Domain.Center, KC.Domain" table="Centers" lazy="true"> 

您可能还需要使用以下在hbm.xml文件中...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        namespace="KC.Domain" 
        assembly="KC.Domain"> 
    <class name="Center" table="Centers" lazy="true"> 

注意命名空间和组件声明。我发现让他们在顶部清理我的hbm.xml文件。那么你可以使用相对的名字。

+0

嘿詹姆斯,谢谢你的回答,但那也行不通。我在开始的时候就是这样,我开始改变它,但是它也没有做到:( – 2010-12-22 05:16:21