我有以下映射:
public webpages_RolesMap()
{
// Primary Key
this.HasKey(t => t.RoleId);
// Properties
this.Property(t => t.RoleName)
.IsRequired()
.HasMaxLength(256);
// Table & Column Mappings
this.ToTable("webpages_Roles");
this.Property(t => t.RoleId).HasColumnName("RoleId");
this.Property(t => t.RoleName).HasColumnName("RoleName");
// Relationships
this.HasMany(t => t.UserProfiles)
.WithMany(t => t.webpages_Roles)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("RoleId");
m.MapRightKey("UserId");
});
}
当我使用Code First时,这会强制EF创建一个如下所示的webpages_UsersInRoles表:
CREATE TABLE [dbo].[webpages_UsersInRoles](
[RoleId] [int] NOT NULL,
[UserId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[RoleId] ASC,
[UserId] 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
但是,Microsoft创建的SimpleMembership类执行不指定列名的插入,并且它希望第一列是UserID,第二列是第二个RoleId。
INSERT INTO webpages_UsersInRoles VALUES (1,3);
如何使上面的映射创建一个表,其中UserID为第1列,RoleId为第2列?
请注意,我已尝试添加此内容:
public partial class UsersInRoles
{
[Key, Column(Order = 0)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
public int RoleId { get; set; }
}
但似乎忽略了这一点,并且仍然以错误的顺序创建了多个列名称。
答案 0 :(得分:0)
我认为您必须从另一方配置多对多关系以更改列顺序:
// UserProfileMap derived from EntityTypeConfiguration<UserProfile>
public UserProfileMap()
{
// ...
this.HasMany(t => t.webpages_Roles)
.WithMany(t => t.UserProfiles)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
}