En una pérdida, cómo asignar dos clases en Nhibernate
-
03-07-2019 - |
Pregunta
Por favor, perdona la pregunta torpe (si puedes encontrar una mejor manera de redactar la pregunta, siéntete libre de editarla).
Tengo dos clases, SupportTicketCategory y SupportTicket (respectivamente):
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;
}
}
}
y
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 estructura de mi tabla es la siguiente:
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]
Básicamente, quiero asignar un SupportTicketCategory al SupportTicket como lo es en mi clase, sin embargo, no puedo averiguar cuál es el tipo de mapeo adecuado y no puedo encontrar un ejemplo de esto en las interwebs.
Actualización: Cambié la propiedad SupportTicketCategory a getters and setters de la vieja escuela y funcionó ... sintaxis de azúcar por pérdida.
Solución
Si usa MyGeneration con el plantilla de NHibernate , puede apuntarlo a su base de datos y hará las asignaciones para usted, para que pueda ver cómo se debe hacer.
Otros consejos
Creo que lo que estás buscando es el "muchos a uno" elemento. (Esto va dentro de su elemento de clase para SupportTicket)
<many-to-one name="SupportTicketCategory" column="SupportTicketCategoryId" update="false" insert="false" />
Se pueden hacer muchos mapeos uno a uno de esta manera:
<many-to-one name="SupportTicketCategory" class="SupportTicketCategory" not-null="false" column="SupportTicketCategoryId" />