不知所措,如何在Nhibernate中映射两个类

时间:2009-04-29 21:14:21

标签: nhibernate nhibernate-mapping

请原谅这个笨拙的问题(如果你能找到一个更好的方式来解释问题,请随意编辑)。

我有两个类SupportTicketCategory和SupportTicket(分别):

   public class SupportTicketCategory
{
    public SupportTicketCategory()
    { }

    private int _supportTicketCategoryID;
    public virtual int SupportTicketCategoryID
    {
        get { return _supportTicketCategoryID; }
        set
        {
            _supportTicketCategoryID = value;
        }
    }

    private string _supportTicketCategoryName;
    public virtual string SupportTicketCategoryName
    {
        get { return _supportTicketCategoryName; }
        set
        {
            _supportTicketCategoryName = value;
        }
    }


}

     public SupportTicket()
    { }

    private int _supportTicketID;
    public virtual int SupportTicketID
    {
        get { return _supportTicketID; }
        set
        {
            _supportTicketID = value;
        }
    }

    private SupportTicketCategory _supportTicketCategory;
    public virtual SupportTicketCategory SupportTicketCategory { get; set; }

我的表格结构如下:

CREATE TABLE [dbo].[supporttickets](
[supportticketid] [int] IDENTITY(1,1) NOT NULL,
[supportticketcategoryid] [int] NOT NULL,
 CONSTRAINT [PK_supporttickets] PRIMARY KEY CLUSTERED 
(
    [supportticketid] ASC
)
) ON [PRIMARY]

ALTER TABLE [dbo].[supporttickets]  
WITH CHECK ADD CONSTRAINT
[FK_supporttickets_supportticketcategories] 
FOREIGN KEY([supportticketcategoryid])
REFERENCES [dbo].[supportticketcategories] ([supportticketcategoryid])

ALTER TABLE [dbo].[supporttickets] CHECK CONSTRAINT  [FK_supporttickets_supportticketcategories]

CREATE TABLE [dbo].[supportticketcategories](
    [supportticketcategoryid] [int] IDENTITY(1,1) NOT NULL,
    [supportticketcategoryname] [varchar](50) NOT NULL,
 CONSTRAINT [PK_supportticketcategories] PRIMARY KEY CLUSTERED 
(
    [supportticketcategoryid] ASC
)
) ON [PRIMARY]

基本上,我想将SupportTicketCategory映射到SupportTicket,就像它在我的类中一样,但是我无法弄清楚什么是正确的映射类型,并且无法在interwebs上找到这样的示例。

更新 我将SupportTicketCategory属性更改为旧学校的getter和setter,它有效...语法糖丢失。

3 个答案:

答案 0 :(得分:1)

如果您将MyGenerationNHibernate template一起使用,则可以将其指向您的数据库并为您制作映射,以便了解应该如何完成。

答案 1 :(得分:0)

我认为你所寻找的是“多对一”元素。 (这是在SupportTicket的类元素内部)

<many-to-one name="SupportTicketCategory" column="SupportTicketCategoryId" update="false" insert="false" />

答案 2 :(得分:0)

可以像这样做多对一的映射:

<many-to-one name="SupportTicketCategory" class="SupportTicketCategory" not-null="false" column="SupportTicketCategoryId" />