我有两张桌子:
分类和 用户
CategoryId 是一个int, UserId 是一个字符串(nvarchar(128))。 我为我的用户提供了这个映射:
this.HasMany(t => t.ForbiddenCategories)
.WithMany(t => t.ForbiddenUsers)
.Map(m =>
{
m.ToTable("UserForbiddenCategories");
m.MapLeftKey("CategoryId");
m.MapRightKey("UserId");
});
但是当我为此创建一个数据库时,它会这样做:
CREATE TABLE [dbo].[UserForbiddenCategories](
[UserId] [int] NOT NULL,
[CategoryId] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.UserForbiddenCategories] PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[CategoryId] 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].[UserForbiddenCategories] WITH CHECK ADD CONSTRAINT [FK_dbo.UserForbiddenCategories_dbo.Categories_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[Categories] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[UserForbiddenCategories] CHECK CONSTRAINT [FK_dbo.UserForbiddenCategories_dbo.Categories_UserId]
GO
ALTER TABLE [dbo].[UserForbiddenCategories] WITH CHECK ADD CONSTRAINT [FK_dbo.UserForbiddenCategories_dbo.IdentityUsers_CategoryId] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[IdentityUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[UserForbiddenCategories] CHECK CONSTRAINT [FK_dbo.UserForbiddenCategories_dbo.IdentityUsers_CategoryId]
GO
这完全是错误的方式。 CategoryId应该是带有FK到类别的int,UserId应该是带有FK到IdentityUsers的字符串。
谁能告诉我我做错了什么?或者我如何告诉EF更改地图?
干杯,
/ r3plica