How to specify a schema for a many-to-many relationship table?

与世无争的帅哥 提交于 2019-12-23 09:30:05

问题


I have a User-Role many-to-many relationship, specified by this excerpt from an EntityTypeConfiguration<Role> derived class that allows me to specifiy schemas for single tables, e.g:

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

Now, for Role, the configuration class contains this code, and the resultant table UserRole gets created under the 'dbo' schema, not my desired schema. This is that code:

[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");
                     });
    }
}

Is there anything I can do, except script a schema change during the seeding phase of initialization, to have table UserRole created under the 'parkpay' schema and not the 'dbo' schema?


回答1:


I don't see why this wouldn't work:

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


来源:https://stackoverflow.com/questions/17113541/how-to-specify-a-schema-for-a-many-to-many-relationship-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!