I’m trying to define a self referencing relationship for a workflow step where that step is not available to be kicked off until all of its dependent steps have been completed. A step can have zero or many dependencies. I have the following but, EF doesn’t map this correctly. Instead it creates the following table which does not meet my needs. How can I achieve this?
Class:
public class WorkflowStepDefinition : EntityBase, IAudited {
[Key]
public int WorkflowStepDefinitionId { get; set; }
[MaxLength(100)]
public string Name { get; set; }
public string Description { get; set; }
public int WorkflowDefinitionId { get; set; }
public virtual WorkflowDefinition WorkflowDefinition { get; set; }
public virtual IList<WorkflowStep> WorkflowSteps { get; set; }
public virtual IList<WorkflowStepDefinition> DependsOn { get; set; }
public AuditDetails Audit { get; set; }
}
Table Def:
CREATE TABLE [dbo].[WorkflowStepDefinitions](
[WorkflowStepDefinitionId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Description] [nvarchar](max) NULL,
[WorkflowDefinitionId] [int] NOT NULL,
[Audit_CreatedBy] [nvarchar](max) NOT NULL,
[Audit_DateCreated] [datetimeoffset](7) NOT NULL,
[Audit_UpdatedBy] [nvarchar](max) NULL,
[Audit_DateUpdated] [datetimeoffset](7) NULL,
[WorkflowStepDefinition_WorkflowStepDefinitionId] [int] NULL,
CONSTRAINT [PK_WorkflowStepDefinitions] PRIMARY KEY CLUSTERED
(
[WorkflowStepDefinitionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[WorkflowStepDefinitions] WITH CHECK ADD CONSTRAINT [FK_WorkflowStepDefinitions_WorkflowDefinitions_WorkflowDefinitionId] FOREIGN KEY([WorkflowDefinitionId])
REFERENCES [dbo].[WorkflowDefinitions] ([WorkflowDefinitionId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[WorkflowStepDefinitions] CHECK CONSTRAINT [FK_WorkflowStepDefinitions_WorkflowDefinitions_WorkflowDefinitionId]
GO
ALTER TABLE [dbo].[WorkflowStepDefinitions] WITH CHECK ADD CONSTRAINT [FK_WorkflowStepDefinitions_WorkflowStepDefinitions_WorkflowStepDefinition_WorkflowStepDefinitionId] FOREIGN KEY([WorkflowStepDefinition_WorkflowStepDefinitionId])
REFERENCES [dbo].[WorkflowStepDefinitions] ([WorkflowStepDefinitionId])
GO
ALTER TABLE [dbo].[WorkflowStepDefinitions] CHECK CONSTRAINT [FK_WorkflowStepDefinitions_WorkflowStepDefinitions_WorkflowStepDefinition_WorkflowStepDefinitionId]
GO
Edit:
Ideally I think I’d like something that produces a join table such as:
CREATE TABLE [dbo].[WorkflowStepDefinitionDependencies](
WorkflowStepDefinitionId int NOT NULL,
DependencyId int NOT NULL
)
As mentioned in comment you can achieve this with fluent API which also gives you possibility to name your junction table and FKs in the junction table: