nHibernate & SQLite = [Class] 未映射

发布于 2024-10-08 08:48:12 字数 3848 浏览 0 评论 0原文

好吧,我现在已经为此奋斗了一段时间,而且我似乎找不到解决方案,它应该非常简单。我得到了一个类(非常简单的类),我得到了 hbm.xml(设置为嵌入资源),我得到了 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

的代码

<?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>

这是 hbm.xml 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>

Well i have been fighting with this for a little bit now, and i can't seem to find a solution for something it should be really simple. I got a class ( really simple class ) i got the hbm.xml ( SET AS EMBEDDED RESOURCE ) i got the config file set for SQLite but i still get the [Class] Is not mapped.

Here is where the error comes up, when i run this:

 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

Code inside my Center Class

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

Here it is the code for the hbm.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>

The 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-10-15 08:48:12

根据您的映射文件,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 文件。然后您可以在整个过程中使用相对名称。

According to your mapping file, the Center class exists in the KC assembly. According to your configuration, the assembly is named KC.Domain. Verify the name of the assembly containing your Center class. Your mapping class probably needs to say:

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

You might also want to use the following in your hbm.xml file...

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

Note the namespace and assembly declarations. I find that having them at the top cleans up my hbm.xml files. Then you can use relative names throughout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文