我正在使用流畅的API映射我的模型代码,以便生成模式。兴趣点是
public class RoleMapping : EntityTypeConfiguration<Role>
{
public RoleMapping()
{
// ...
this.HasMany(r => r.Permissions).WithMany();
}
}
我希望Roles表,Perimssions表和中间表。但是,除非我添加一个Roles导航属性,否则执行Permission类我只生成两个表:
CREATE TABLE [Roles](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL )
GO
CREATE TABLE [Permissions](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Role_Id] [int] NULL)
GO
ALTER TABLE [dbo].[Permissions] WITH CHECK
ADD CONSTRAINT [Role_Permissions] FOREIGN KEY([Role_Id])
REFERENCES [dbo].[Roles] ([Id])
GO
ALTER TABLE [dbo].[Permissions] CHECK CONSTRAINT [Role_Permissions]
GO
为什么它会那样工作?
答案 0 :(得分:0)
我缺少将映射添加到模型构建器:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new RoleMapping());
modelBuilder.Configurations.Add(new PermissionMapping());
// other mapping configurations
}