不要在持久性测试中指定列

时间:2012-01-25 09:30:43

标签: nhibernate mapping fluent

我有一对多关系但是,在我的域实体中,我不希望该关联是双向的。 映射没问题:" hasmany"在一边而没有任何东西(而不是"参考"在另一边)。

但我的问题是持久性测试: 我必须指定"参考"的列。一边等等,我需要班上的一个属性 (当然,列 - 在数据库中 - 不允许空值,但是是外键)。

有没有办法在没有属性的情况下在持久性测试中指定列? (数据库专栏,例如)

下面的类,表,映射和测试。非常感谢!

Public Class Order
    Public Overridable Property OrderId As Integer
    Public Overridable Property OrderDate As Date
    Public Overridable Property TotalOrderAmount As Long
    Public Overridable Property OrderLines As IList(Of OrderLine)
End Class

Public Class OrderLine
    Public Overridable Property OrderLineId As Integer
    Public Overridable Property OrderLineAmount As Long
    'I don't want to create property "OrderParent As Order" because I don't need it
End Class

TABLE [dbo].[tblOrders]
    [OrderId] [int] NOT NULL,
    [OrderDate] [datetime] NOT NULL,
    [TotalOrderAmount] [bigint] NOT NULL,
CONSTRAINT [PK_tblOrders] PRIMARY KEY CLUSTERED 


TABLE [dbo].[tblOrderLines](
    [OrderLineId] [int] NOT NULL,
    [OrderId] [int] NOT NULL,
    [OrderLineAmount] [bigint] NOT NULL,
CONSTRAINT [PK_tblOrderLines] PRIMARY KEY CLUSTERED 

Public Class OrderMap
    Inherits ClassMap(Of Order)

    Public Sub New()
        Table("tblOrders")

        Id(Function(x) x.OrderId).GeneratedBy.HiLo("1000")

        Map(Function(x) x.OrderDate)
        Map(Function(x) x.TotalOrderAmount)

        HasMany(Function(x) x.OrderLines) _
            .Inverse() _
            .Cascade.All()
    End Sub
End Class

Public Class OrderLineMap
    Inherits ClassMap(Of OrderLine)

    Public Sub New()
        Table("tblOrderLines")

        Id(Function(x) x.OrderLineId).GeneratedBy.HiLo("1000")

        Map(Function(x) x.OrderLineAmount)
    End Sub
End Class

Public Class OrderPersistenceTest
…
        Dim ps As PersistenceSpecification(Of Order)
        ps.CheckProperty(Function(x) x.OrderDate, Date.Today) _
            .CheckProperty(Function(x) x.TotalOrderAmount, 1000000L) _
            .VerifyTheMappings()
End Class

Public Class OrderLinePersistenceTest
        'Fails because I don't specify value for foreign key OrderId
        Dim ps As PersistenceSpecification(Of OrderLine)
        ps.CheckProperty(Function(x) x.OrderLineAmount, 1000000L) _
            .VerifyTheMappings()
End Class

0 个答案:

没有答案