0

我正在尝试首先在 asp.net 会员提供商上进行编码。当代码第一次创建表时,它会生成所有表以及与桥表的适当关系,但它还会创建从 AspNet_User(one) 到 Aspnet_Role(many) 的附加关系。你知道它为什么这样做吗?用户和角色表之间不应该有任何关系。

public class Aspnet_Role
{
    public Aspnet_Role()
    {
        Aspnet_Users = new HashSet<Aspnet_Users>();
    }
    [Key]
    public Guid RoleId { get; set; }
    public string RoleName { get; set; }
    public string LoweredRoleName { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Aspnet_Users> Aspnet_Users { get; set; }
}
public class Aspnet_Users
{
    public Aspnet_Users()
    {
        Aspnet_Roles = new HashSet<Aspnet_Role>();
    }
    [Key]
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public string LoweredUserName { get; set; }
    public string MobileAlias { get; set; }
    public bool IsAnonymous { get; set; }
    public DateTime LastActivityDate { get; set; }

    public virtual Aspnet_Membership Aspnet_Membership { get; set; }
    public virtual ICollection<Aspnet_Role> Aspnet_Roles { get; set; }
}
public class StagingContext : DbContext
{
    public DbSet<Aspnet_Role> Aspnet_Roles { get; set; }
    public DbSet<Aspnet_Users> Aspnet_Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)

    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Aspnet_Users>()
            .HasMany(r => r.Aspnet_Roles)
            .WithMany()
            .Map(m => m.ToTable("aspnet_UsersInRoles")
                .MapRightKey("RoleId")
                .MapLeftKey("UserId"));

    }
}
4

1 回答 1

0

我在 WithMany 扩展上添加了导航属性。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    base.OnModelCreating(modelBuilder);

     modelBuilder.Entity<Aspnet_Users>()
         .HasMany(r => r.Aspnet_Roles)
         .WithMany(u => u.Aspnet_Users)
         .Map(m => m.ToTable("aspnet_UsersInRoles")
         .MapRightKey("RoleId")
          .MapLeftKey("UserId")); 

}
于 2013-02-20T20:09:54.250 回答