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

时间:2022-06-13 08:55:41

Please forgive the clumsy question (if you can figure out a better way to word the question feel free to edit away).

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

I have two classes SupportTicketCategory and SupportTicket (respectively):

我有两个类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;
        }
    }


}

and

     public SupportTicket()
    { }

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

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

My table structure is as follows:

我的表结构如下:

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]

So basically, I want to map a SupportTicketCategory onto the SupportTicket like it is in my class, however I cannot figure out what is the proper mapping type and cannot find an example of this on the interwebs.

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

Update: I changed the SupportTicketCategory property to old school getters and setters and it worked...syntax sugar for loss.

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

3 个解决方案

#1


If you use MyGeneration with the NHibernate template, you can point it at your database and it will make the mappings for you, so you can see how it ought to be done.

如果你将MyGeneration与NHibernate模板一起使用,你可以将它指向你的数据库并为你制作映射,这样你就可以看到它应该如何完成。

#2


I think what you're looking for is the "many-to-one" element. (This goes inside your class element for SupportTicket)

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

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

#3


Many to one mappings can be done like this:

可以像这样完成多对一映射:

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

#1


If you use MyGeneration with the NHibernate template, you can point it at your database and it will make the mappings for you, so you can see how it ought to be done.

如果你将MyGeneration与NHibernate模板一起使用,你可以将它指向你的数据库并为你制作映射,这样你就可以看到它应该如何完成。

#2


I think what you're looking for is the "many-to-one" element. (This goes inside your class element for SupportTicket)

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

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

#3


Many to one mappings can be done like this:

可以像这样完成多对一映射:

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