In perdita, come mappare due classi a Nhibernate
-
03-07-2019 - |
Domanda
Per favore, perdona la domanda goffa (se riesci a trovare un modo migliore per pronunciare la domanda, sentiti libero di modificarla).
Ho due classi SupportTicketCategory e SupportTicket (rispettivamente):
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;
}
}
}
e
public SupportTicket()
{ }
private int _supportTicketID;
public virtual int SupportTicketID
{
get { return _supportTicketID; }
set
{
_supportTicketID = value;
}
}
private SupportTicketCategory _supportTicketCategory;
public virtual SupportTicketCategory SupportTicketCategory { get; set; }
La mia struttura della tabella è la seguente:
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]
Quindi, fondamentalmente, voglio mappare una SupportTicketCategory sul SupportTicket come è nella mia classe, tuttavia non riesco a capire quale sia il tipo di mappatura corretto e non riesco a trovare un esempio di questo negli interwebs.
Aggiornamento: Ho cambiato la proprietà SupportTicketCategory in getter e setter della vecchia scuola e ha funzionato ... sintassi zucchero per perdita.
Soluzione
Se si utilizza MyGeneration con modello NHibernate , puoi puntarlo sul tuo database e faremo le mappature per te, così puoi vedere come dovrebbe essere fatto.
Altri suggerimenti
Penso che quello che stai cercando sia il "molti-a-uno" elemento. (Questo va inserito nel tuo elemento di classe per SupportTicket)
<many-to-one name="SupportTicketCategory" column="SupportTicketCategoryId" update="false" insert="false" />
Molti mapping uno a uno possono essere fatti in questo modo:
<many-to-one name="SupportTicketCategory" class="SupportTicketCategory" not-null="false" column="SupportTicketCategoryId" />