0

次のマッピングがあります。

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 で 2 番目の 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; }

}

しかし、これを無視して、列名が間違った順序で多対多を作成しているようです。

4

1 に答える 1

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");
        });
}
于 2013-03-18T21:20:53.700 に答える