2013-03-17 24 views
0

我有以下映射:當EF代碼第一次創建它時,是否有方法可以爲許多表指定列順序?

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

     } 

當我使用代碼第一次那麼這迫使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 

但是微軟創建的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

我認爲你必須配置從對方的許多一對多的關係,以更改列的順序:

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