0

我得到这个项目是一个操作员可以有很多角色。让我从代码开始,这样你就可以明白我的意思了。

TD运营商:

[DisplayName("Operator")]
public class TDOperator
{


    public int TDOperatorId { get; set; }


    [DisplayName("First Name")]
    [Required]
    public string FirstName { get; set; }

    [Required]
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [DisplayName("Email Adress")]
    public string EmailAddress { get; set; }




    [Required]
    public string Username { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [HiddenInput(DisplayValue = true)]
    public DateTime LastLogin { get; set; }



    public List<string> RoleNames
    {
        get
        {
            return Roles.Select(x => x.Name).ToList();

        }
    }


    public virtual ICollection<Role> Roles { get; set; }



    [Required]
    [DisplayName("Topdesk Username")]
    public string TDUserName { get; set; }

    public virtual ICollection<TDOperator> Coaches { get; set; }
    public List<string> CoachNames
    {
        get
        {
            return Coaches.Select(x => x.ToString()).ToList();

        }
    }
    public virtual ICollection<TDOperator> Pupils { get; set; }
    public List<string> PupilNames
    {
        get
        {
            return Pupils.Select(x => x.ToString()).ToList();

        }
    }






    public override string ToString()
    {
        return this.FirstName + " " + this.LastName;
    }
}

还有一个角色

public class Role
{
    public int RoleID { get; set; }
    public string Name { get; set; }
    public int Priority { get; set; }
}

对不起,我打算稍后清理混乱的代码。

当我运行它时,它生成了一个包含 3 个表和列的数据库:

 - TDOperatorTDOperators
  1.TDOperator_TDOperatorId
  2.TDOperator_TDOperatorId1


 - TDOperators
  1.TDOperatorId
  2.FirstName
  3.LastName
  4.EmailAdress
  5.UserName
  6.PassWord
  7.LastLogin
  8.TDUsername 


 - Roles
  1.RoleId
  2.Name
  3.Priority
  4.TDOperator_TDOperatorId <<--- WTF!

问题是我现在只能将角色分配给 1 个用户。例如,只能有 1 个管理员和 1 个用户。它应该创建和额外的外键表,但它没有!

请帮忙。

4

1 回答 1

1

您没有告诉 Entity Framework 您想要 Roles -> Operators 关系。尝试将此添加到您的Role课程中。

public virtual ICollection<TDOperator> Operators { get; set; }
于 2013-04-05T15:15:15.770 回答