EF Code First 4.3自我引用零到多

时间:2012-03-23 12:26:13

标签: entity-framework-4 ef-code-first code-first entity-framework-4.3

我正在尝试为工作流步骤定义自引用关系,其中该步骤无法在其所有相关步骤完成之前启动。步骤可以具有零个或多个依赖性。我有以下但是,EF没有正确映射这个。相反,它创建了下表,不符合我的需要。我怎样才能做到这一点?

类别:

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; }        
}

表格默认

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

修改 理想情况下,我认为我想要产生连接表的东西,例如:

CREATE TABLE [dbo].[WorkflowStepDefinitionDependencies](
    WorkflowStepDefinitionId int NOT NULL,
    DependencyId int NOT NULL
)

1 个答案:

答案 0 :(得分:2)

如评论中所述,您可以使用流畅的API实现此目的,这也使您可以在联结表中命名联结表和FK:

modelBuilder.Entity<WorkflowStepDefinition>()
            .HasMany(w => w.DependsOn)
            .WithMany()
            .Map(mc =>
                {
                     mc.ToTable("WorkflowStepDefinitionDependencies", "dbo");
                     mc.MapLeftKey("WorkflowStepDefinitionId");
                     mc.MapRightKey("DependencyId");
                });