代码第一流畅的api - 表关系

时间:2014-03-19 09:49:20

标签: entity-framework asp.net-mvc-4 ef-code-first

我有以下表格,需要设置它们之间的关系。

enter image description here

表的模型类如给出

public class UserAction
{
    public int ActionID { get; set; }
    public string ActionName { get; set; }

    public virtual ICollection<RoleScreenActionPermission> RoleScreenActionPermissions { get; set; }
}
 public class Screen
{
    public int ScreenID { get; set; }
    public string ScreenName { get; set; }

    public virtual ICollection<RoleScreenActionPermission> RoleScreenActionPermissions { get; set; }
}
public class ScreenAction
{
    public int ScreenActionID { get; set; }
    public int ScreenID { get; set; }
    public int ActionID { get; set; }

    public virtual Screen Screen { get; set; }
    public virtual UserAction UserAction { get; set; }
}
public class RoleScreenActionPermission
{
    public int RoleScreenActionPermissionID { get; set; }
    public int ScreenActionID { get; set; }
    public int RoleID { get; set; }

    public virtual ScreenAction ScreenAction { get; set; }
    public virtual Role Role { get; set; }

}

创建的talbe结构如下:  enter image description here

请帮助正确设置关系。

1 个答案:

答案 0 :(得分:2)

尝试从类中删除所有自己的外键。 EF必须成功。

UPD:

public class Screen
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Action
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ScreenAction
{
    public int Id { get; set; }
    public virtual Screen Screen { get; set; }
    public virtual Action Action { get; set; }
}

public class RoleScreenActionPermission
{
    public int Id { get; set; }
    public virtual ScreenAction ScreenAction { get; set; }
    public virtual Role Role { get; set; }
}