我有一个关于实体框架的问题。我创建了一个数据库,然后使用 Entity Framework Power Tools 使用 Reverse Engineer Code First 选项创建数据库实体的一些模型。产生的关系不是我所期望的。
在数据库中,我有四个表:Work、WorkType、WorkCategory、Person。WorkType、WorkCategory 和 Person 都是与 Work 表的一对多关系,使用 Work 表中的外键。
WorkType 和 WorkCategory 关系很好,但是 Work 到 Person 的关系被忽略了。
我这样说是因为在 work.cs 模型类中没有称为 Person 的强类型虚拟属性(与 WorkType 和 WorkCategory 一样),并且在 WorkMap.cs 文件中没有定义关系(定义了 WorkType 和 WorkCategory 关系)。
请有人能解释我错过了什么吗?谢谢!
生成的 Work.cs 模型类:
public partial class Work
{
public Work()
{
this.Comments = new List<Comment>();
this.Files = new List<File>();
}
public int WorkId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string TechnicalInfo { get; set; }
public int PersonId { get; set; }
public int WorkTypeId { get; set; }
public int WorkCategoryId { get; set; }
public System.DateTime Created { get; set; }
public System.DateTime Updated { get; set; }
public bool Deleted { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<File> Files { get; set; }
public virtual WorkCategory WorkCategory { get; set; }
public virtual WorkType WorkType { get; set; }
}
EF Power Tools 生成的 workmap.cs 映射:
public class WorkMap : EntityTypeConfiguration<Work>
{
public WorkMap()
{
// Primary Key
this.HasKey(t => t.WorkId);
// Properties
this.Property(t => t.Title)
.IsRequired()
.HasMaxLength(300);
this.Property(t => t.Description)
.IsRequired()
.HasMaxLength(3000);
this.Property(t => t.TechnicalInfo)
.HasMaxLength(400);
// Relationships
this.HasRequired(t => t.WorkCategory)
.WithMany(t => t.Works)
.HasForeignKey(d => d.WorkCategoryId);
this.HasRequired(t => t.WorkType)
.WithMany(t => t.Works)
.HasForeignKey(d => d.WorkTypeId);
}
}
SQL 创建显示关系的脚本:
CREATE TABLE [dbo].[Person](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](300) NOT NULL,
[Description] [varchar](4000) NULL,
[EmailAddress] [varchar](250) NOT NULL,
[Password] [varchar](200) NOT NULL,
[ReputationPoints] [int] NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Deleted] [bit] NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[PersonId] 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
CREATE TABLE [dbo].[WorkType](
[WorkTypeId] [int] IDENTITY(1,1) NOT NULL,
[WorkTypeDesc] [varchar](200) NOT NULL,
CONSTRAINT [PK_WorkType] PRIMARY KEY CLUSTERED
(
[WorkTypeId] 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
CREATE TABLE [dbo].[WorkCategory](
[WorkCategoryId] [int] IDENTITY(1,1) NOT NULL,
[WorkCategoryDesc] [varchar](200) NOT NULL,
[WorkTypeId] [int] NOT NULL,
CONSTRAINT [PK_WorkCategory] PRIMARY KEY CLUSTERED
(
[WorkCategoryId] 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
CREATE TABLE [dbo].[Work](
[WorkId] [int] IDENTITY(1,1) NOT NULL,
[Title] [varchar](300) NOT NULL,
[Description] [varchar](3000) NOT NULL,
[TechnicalInfo] [varchar](400) NULL,
[PersonId] [int] NOT NULL,
[WorkTypeId] [int] NOT NULL,
[WorkCategoryId] [int] NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Deleted] [bit] NOT NULL,
CONSTRAINT [PK_Work] PRIMARY KEY CLUSTERED
(
[WorkId] 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
CREATE TABLE [dbo].[File](
[FileId] [int] IDENTITY(1,1) NOT NULL,
[Filename] [varchar](500) NULL,
[Url] [varchar](3000) NULL,
[WorkId] [int] NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Deleted] [bit] NOT NULL,
CONSTRAINT [PK_File] PRIMARY KEY CLUSTERED
(
[FileId] 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
CREATE TABLE [dbo].[Comment](
[CommentId] [int] IDENTITY(1,1) NOT NULL,
[CommentText] [varchar](2000) NOT NULL,
[PersonId] [int] NOT NULL,
[WorkId] [int] NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Deleted] [bit] NOT NULL,
CONSTRAINT [PK_Comment] PRIMARY KEY CLUSTERED
(
[CommentId] 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].[Work] WITH CHECK ADD CONSTRAINT [FK_Work_Person] FOREIGN KEY([PersonId])
REFERENCES [dbo].[Person] ([PersonId])
GO
ALTER TABLE [dbo].[Work] CHECK CONSTRAINT [FK_Work_Person]
GO
ALTER TABLE [dbo].[Work] WITH CHECK ADD CONSTRAINT [FK_Work_WorkCategory] FOREIGN KEY([WorkCategoryId])
REFERENCES [dbo].[WorkCategory] ([WorkCategoryId])
GO
ALTER TABLE [dbo].[Work] CHECK CONSTRAINT [FK_Work_WorkCategory]
GO
ALTER TABLE [dbo].[Work] WITH CHECK ADD CONSTRAINT [FK_Work_WorkType] FOREIGN KEY([WorkTypeId])
REFERENCES [dbo].[WorkType] ([WorkTypeId])
GO
ALTER TABLE [dbo].[Work] CHECK CONSTRAINT [FK_Work_WorkType]
GO
ALTER TABLE [dbo].[File] WITH CHECK ADD CONSTRAINT [FK_File_Work] FOREIGN KEY([WorkId])
REFERENCES [dbo].[Work] ([WorkId])
GO
ALTER TABLE [dbo].[File] CHECK CONSTRAINT [FK_File_Work]
GO
ALTER TABLE [dbo].[Comment] WITH CHECK ADD CONSTRAINT [FK_Comment_Person] FOREIGN KEY([PersonId])
REFERENCES [dbo].[Person] ([PersonId])
GO
ALTER TABLE [dbo].[Comment] CHECK CONSTRAINT [FK_Comment_Person]
GO
ALTER TABLE [dbo].[Comment] WITH CHECK ADD CONSTRAINT [FK_Comment_Work] FOREIGN KEY([WorkId])
REFERENCES [dbo].[Work] ([WorkId])
GO
ALTER TABLE [dbo].[Comment] CHECK CONSTRAINT [FK_Comment_Work]
GO