What is the proper way to create a recursive entity in the Entity Framework?

Posted by Orion Adrian on Stack Overflow See other posts from Stack Overflow or by Orion Adrian
Published on 2010-03-09T16:23:44Z Indexed on 2010/03/11 20:14 UTC
Read the original article Hit count: 358

Filed under:
|
|

I'm currently using VS 2010 RC, and I'm trying to create a model that contains a recursive self-referencing entity. Currently when I import the entity from the model I get an error indicating that the parent property cannot be part of the association because it's set to 'Computed' or 'Identity', though I'm not sure why it does it that way. I've been hand-editing the file to get around that error, but then the model simply doesn't work.

What is the proper way to get recursive entities to work in the Entity Framework.

CREATE TABLE [dbo].[Appointments](
    [AppointmentId] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](1024) NULL,
    [Start] [datetime] NOT NULL,
    [End] [datetime] NOT NULL,
    [Username] [varchar](50) NOT NULL,
    [RecurrenceRule] [nvarchar](1024) NULL,
    [RecurrenceState] [varchar](20) NULL,
    [RecurrenceParentId] [int] NULL,
    [Annotations] [nvarchar](50) NULL,
    [Application] [nvarchar](100) NOT NULL,
    CONSTRAINT [PK_Appointments] PRIMARY KEY CLUSTERED 
    (
        [AppointmentId] ASC
    )
)
GO

ALTER TABLE [dbo].[Appointments]  WITH CHECK ADD  CONSTRAINT [FK_Appointments_ParentAppointments] FOREIGN KEY([RecurrenceParentId])
REFERENCES [dbo].[Appointments] ([AppointmentId])
GO

ALTER TABLE [dbo].[Appointments] CHECK CONSTRAINT [FK_Appointments_ParentAppointments]
GO

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about c#