views:

133

answers:

3

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):

   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.

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

+1  A: 

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.

George Stocker
Downloading now...
Webjedi
If you are not working with a legacy database, I suggest you to create the database from the mapping file, not vice versa. This is called Domain Driven Development, because you are writing you domain classes first, specify the mapping afterwards, and the database schema could be created from that. You can do this simply with Nhibernates SchemaExport tool.
Stefan Steinegger
I tried doing as Stefan recommends using the many to one mapping as shown in examples below...then populating the tables and it still fails to populate the SupportTicketCategory property.
Webjedi
A: 

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

<many-to-one name="SupportTicketCategory" column="SupportTicketCategoryId" update="false" insert="false" />
GalacticCowboy
It's coming back null even though there is a SupportTicket with a supportticketcategoryid=1 and a corresponding record of SupportTicketCategory with a supportticketcategoryID=1... maybe there is something else at work here.
Webjedi
Are you using the fully qualified assembly name in your mapping file like ...class="SupportTicketCategory, My.Namespace"... ?
Flo
It should actually work, but why update=false and insert=false?
Stefan Steinegger
@Stefan: copied from another project, and that's how it was there...
GalacticCowboy
A: 

Many to one mappings can be done like this:

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