【问题标题】:How can I map NHibernate domain classes with an existing table如何将 NHibernate 域类与现有表映射
【发布时间】:2010-09-15 17:29:58
【问题描述】:

我目前正在尝试在我没有权限在 SQL 2005 中创建 TABLE 的项目中使用 NHibernate。

我不断收到映射错误,我的假设是我没有通过生成表来设置表。我是否可以假设 NHibernate 只能成功映射由其工具生成的表?

如果不是,我如何确保它适用于现有表?


9 月 14 日 响应要求提供更多详细信息:

<id name="MeetingID" column="MeetingID" type="int">
  <generator class="identity" />
</id>
<property name="Description" />
<property name="OwnerNetworkID"  />
<property name="StartDate" />
<property name="EndDate" />
<property name="DayStartHour" />
<property name="DayStartMinute" />
<property name="DayEndHour" />
<property name="DayEndMinute" />
<property name="BreakStartHour" />
<property name="BreakStartMinute" />
<property name="BreakEndHour" />
<property name="BreakEndMinute" />
<property name="IsActive" />
<property name="SessionIntervalMinutes" />
<property name="PeoplePerSlot" />
<property name="DateCreated" />
<property name="LastModified" />
<property name="UpdatedBy" />
<property name="ChangeTimestamp" />
<property name="ClosedForBookingDaysPrior" />
<property name="DefaultMeetingRoom" />

这是课程:

命名空间我的命名空间 公开课

    Private _MeetingID As Integer
    Private _OwnerNetworkID As String
    Private _Description As String

    Public Overridable Property MeetingID() As Integer
        Get
            Return _MeetingID
        End Get
        Set(ByVal value As Integer)
            _MeetingID = value
        End Set
    End Property


    Public Overridable Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Public Overridable Property OwnerNetworkID() As String
        Get
            Return _OwnerNetworkID
        End Get
        Set(ByVal value As String)
            _OwnerNetworkID = value
        End Set
    End Property

    Private _StartDate As Date
    Public Overridable Property StartDate() As Date
        Get
            Return _StartDate
        End Get
        Set(ByVal value As Date)
            _StartDate = value
        End Set
    End Property


    Private _EndDate As Date
    Public Overridable Property EndDate() As Date
        Get
            Return _EndDate
        End Get
        Set(ByVal value As Date)
            _EndDate = value
        End Set
    End Property

    Private _DayStartHour As Byte
    Public Overridable Property DayStartHour() As Byte
        Get
            Return _DayStartHour
        End Get
        Set(ByVal value As Byte)
            _DayStartHour = value
        End Set
    End Property

    Private _DayStartMinute As Byte
    Public Overridable Property DayStartMinute() As Byte
        Get
            Return _DayStartMinute
        End Get
        Set(ByVal value As Byte)
            _DayStartMinute = value
        End Set
    End Property

    Private _DayEndHour As Byte
    Public Overridable Property DayEndHour() As Byte
        Get
            Return _DayEndHour
        End Get
        Set(ByVal value As Byte)
            _DayEndHour = value
        End Set
    End Property

    Private _DayEndMinute As Byte
    Public Overridable Property DayEndMinute() As Byte
        Get
            Return _DayEndMinute
        End Get
        Set(ByVal value As Byte)
            _DayEndMinute = value
        End Set
    End Property

    Private _BreakStartHour As Byte
    Public Overridable Property BreakStartHour() As Byte
        Get
            Return _BreakStartHour
        End Get
        Set(ByVal value As Byte)
            _BreakStartHour = value
        End Set
    End Property

    Private _BreakStartMinute As Byte
    Public Overridable Property BreakStartMinute() As Byte
        Get
            Return _BreakStartMinute
        End Get
        Set(ByVal value As Byte)
            _BreakStartMinute = value
        End Set
    End Property

    Private _BreakEndHour As Byte
    Public Overridable Property BreakEndHour() As Byte
        Get
            Return _BreakEndHour
        End Get
        Set(ByVal value As Byte)
            _BreakEndHour = value
        End Set
    End Property

    Private _BreakEndMinute As Byte
    Public Overridable Property BreakEndMinute() As Byte
        Get
            Return _BreakEndMinute
        End Get
        Set(ByVal value As Byte)
            _BreakEndMinute = value
        End Set
    End Property


    Private _IsActive As Byte
    Public Overridable Property IsActive() As Byte
        Get
            Return _IsActive
        End Get
        Set(ByVal value As Byte)
            _IsActive = value
        End Set
    End Property


    Private _SessionIntervalMinutes As Byte
    Public Overridable Property SessionIntervalMinutes() As Byte
        Get
            Return _SessionIntervalMinutes
        End Get
        Set(ByVal value As Byte)
            _SessionIntervalMinutes = value
        End Set
    End Property

    Private _PeoplePerSlot As Short
    Public Overridable Property PeoplePerSlot() As Short
        Get
            Return _PeoplePerSlot
        End Get
        Set(ByVal value As Short)
            _PeoplePerSlot = value
        End Set
    End Property

    Private _DateCreated As Date
    Public Overridable Property DateCreated() As Date
        Get
            Return _DateCreated
        End Get
        Set(ByVal value As Date)
            _DateCreated = value
        End Set
    End Property
    Private _LastModified As Date
    Public Overridable Property LastModified() As Date
        Get
            Return _LastModified
        End Get
        Set(ByVal value As Date)
            _LastModified = value
        End Set
    End Property

    Private _UpdatedBy As String
    Public Overridable Property UpdatedBy() As String
        Get
            Return _UpdatedBy
        End Get
        Set(ByVal value As String)
            _UpdatedBy = value
        End Set
    End Property

    Private _ChangeTimestamp As Byte()
    Public Overridable Property ChangeTimestamp() As Byte()
        Get
            Return _ChangeTimestamp
        End Get
        Set(ByVal value As Byte())
            _ChangeTimestamp = value
        End Set
    End Property

    Private _ClosedForBookingDaysPrior As Byte
    Public Overridable Property ClosedForBookingDaysPrior() As Byte
        Get
            Return _ClosedForBookingDaysPrior
        End Get
        Set(ByVal value As Byte)
            _ClosedForBookingDaysPrior = value
        End Set
    End Property

    Private _DefaultMeetingRoom As String
    Public Overridable Property DefaultMeetingRoom() As String
        Get
            Return _DefaultMeetingRoom
        End Get
        Set(ByVal value As String)
            _DefaultMeetingRoom = value
        End Set
    End Property




End Class

结束命名空间

【问题讨论】:

  • 您的假设是错误的 :) Nhibernate 适用于现有表,它不会创建表。你能粘贴你的映射吗?
  • @Claudio NHibernate 可以创建表,如果您执行 SchemaExport.Create(true, true);
  • @user 你能给我们看一个例子吗?当然可以使用现有的表,我们只需要看看你在哪里犯了错误。 :)
  • @Rafael Belliard:感谢您的澄清。这是一个角落案例。用户被困在一个更简单的场景中:)
  • 嗨 Huys,我已经提供了上面的映射和 vb 类。也许你可以告诉我我的映射做错了什么。如果您也可以向我推荐我可以在现有表上使用 NHibernate 而无需重新生成的示例,这将有所帮助。

标签: nhibernate nhibernate-mapping


【解决方案1】:

我找到了问题的根源。

这个错误是由于我在调用 AddAssembly(typeof(Type)) 之前没有调用 Configure() 方法造成的。当我创建 SessionFactory 对象时会发生这种情况。

这是这个解决方案的 VB 示例:

之前

    Private Shared ReadOnly Property SessionFactory() As ISessionFactory
        Get
            If _sessionFactory Is Nothing Then
                Dim configuration As New Configuration
                  configuration.AddAssembly(GetType(Meeting).Assembly)
                _sessionFactory = configuration.BuildSessionFactory

            End If
            Return _sessionFactory
        End Get
    End Property

之后

    Private Shared ReadOnly Property SessionFactory() As ISessionFactory
        Get
            If _sessionFactory Is Nothing Then
                Dim configuration As New Configuration

                //Was missing
                configuration.Configure()
                //Configure 
                configuration.AddAssembly(GetType(Meeting).Assembly)
                _sessionFactory = configuration.BuildSessionFactory

            End If
            Return _sessionFactory
        End Get
    End Property

【讨论】:

    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多