如何为多对多关系表指定架构?

时间:2013-06-14 16:51:45

标签: entity-framework ef-code-first entity-framework-5

我有一个用户角色多对多关系,由EntityTypeConfiguration<Role>派生类的摘录指定,允许我为单个表指定模式,例如:

[Schema(SchemaNames.ParkPay)]
class WeekDayConfig : EntityTypeConfigurationWithSchema<WeekDay>
{
    internal WeekDayConfig()
    {
        Ignore(t => t.IsDeleted);
        Property(p => p.Name)
            .IsRequired()
            .HasMaxLength(20);
    }
}

现在,对于Role,配置类包含此代码,结果表UserRole在“dbo”架构下创建,而不是我想要的架构。这就是代码:

[Schema(SchemaNames.ParkPay)]
internal class RoleConfig : EntityTypeConfigurationWithSchema<Role>
{
    public RoleConfig()
    {
        HasMany(t => t.Users)
            .WithMany(t => t.Roles)
            .Map(m =>
                     {
                         m.ToTable("UserRole");                            
                         m.MapLeftKey("RoleId");
                         m.MapRightKey("UserId");
                     });
    }
}

我能做些什么,除了在初始化的播种阶段更改架构脚本,在'parkpay'架构下创建表UserRole而不是'dbo'架构?

1 个答案:

答案 0 :(得分:10)

我不明白为什么这不起作用:

public RoleConfig()
{
    HasMany(t => t.Users)
    .WithMany(t => t.Roles)
    .Map(m =>
    {
        m.ToTable("UserRole","parkpay");                            
        m.MapLeftKey("RoleId");
        m.MapRightKey("UserId");
    });
}
相关问题