Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我有4个类和类属性,但是我应该如何关联这些类?(一个一个多…)_C#_Sql_Entity Framework - Fatal编程技术网

C# 我有4个类和类属性,但是我应该如何关联这些类?(一个一个多…)

C# 我有4个类和类属性,但是我应该如何关联这些类?(一个一个多…),c#,sql,entity-framework,C#,Sql,Entity Framework,哪个表应该与哪个表关联 如何添加外键 我很困惑。你能帮我吗 用户和部门-->一对多 public class User { public int UserId { get; set; } public string Username { get; set; } public string Email { get; set; } public string Password { get; set; }

哪个表应该与哪个表关联

如何添加外键

我很困惑。你能帮我吗

用户和部门-->一对多

  public class User
    {
        public int UserId { get; set; } 
        public string Username { get; set; } 
        public string Email { get; set; } 
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int DepartmentId { get; set; }
        public int TitleId { get; set; }
        public int ManagerUserId { get; set; }
    }
    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentCode { get; set; }
        public string Name { get; set; }
        public int ManagerDepartmentId { get; set; }
        public int ManagerUserId { get; set; }
    }
    public class Position
    {
        public int PositionId { get; set; }
        public string PositionCode { get; set; }
        public string Name { get; set; }
        public int UserId { get; set; }
        public byte Status { get; set; }
    }
    public class Title
    {
        public int TitleId { get; set; }
        public string Name { get; set; }
        public byte IsIntegrationData { get; set; }
    }

用户和部门是一对多关系。 我建议你看看这里:
用户和部门是一对多关系。 我建议你看看这里:

试试这样的方法:

  public class User
    {
        public int UserId { get; set; } 
        public string Username { get; set; } 
        public string Email { get; set; } 
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [ForeignKey("Department")]
        public int DepartmentId { get; set; }
        public virtual Department Department {get; set;}
        
        [ForeignKey("Title")]
        public int TitleId { get; set; }
        public virtual Title Title {get;set;}

        [ForeignKey("Manager")]
        public int ManagerUserId { get; set; }
        public virtual User Manager {get; set;}
        
        [InverseProperty("Manager")]
        public virtual List<Department> DepartmentsManaged {get; set;}
    }


    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentCode { get; set; }
        public string Name { get; set; }

        [ForeignKey("Manager")]
        public int ManagerUserId { get; set; }
        public virtual User Manager {get; set;}

        public virtual List<User> Users {get; set;}
    }


    public class Position
    {
        public int PositionId { get; set; }
        public string PositionCode { get; set; }
        public string Name { get; set; }

        [Required]
        [ForeignKey("User")]
        public int UserId { get; set; }
        public virtual User User {get; set;}

        public byte Status { get; set; }
    }
    public class Title
    {
        public int TitleId { get; set; }
        public string Name { get; set; }
        public byte IsIntegrationData { get; set; }

        public virtual List<User> Users {get; set;}
    }
公共类用户
{
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
[外键(“部门”)]
public int DepartmentId{get;set;}
公共虚拟部门部门{get;set;}
[外键(“标题”)]
公共int TitleId{get;set;}
公共虚拟标题{get;set;}
[外籍钥匙(“经理”)]
public int ManagerUserId{get;set;}
公共虚拟用户管理器{get;set;}
[反向财产(“经理”)]
公共虚拟列表部门托管{get;set;}
}
公共课系
{
public int DepartmentId{get;set;}
公共字符串部门代码{get;set;}
公共字符串名称{get;set;}
[外籍钥匙(“经理”)]
public int ManagerUserId{get;set;}
公共虚拟用户管理器{get;set;}
公共虚拟列表用户{get;set;}
}
公共阶级地位
{
public int PositionId{get;set;}
公共字符串位置代码{get;set;}
公共字符串名称{get;set;}
[必需]
[外键(“用户”)]
public int UserId{get;set;}
公共虚拟用户用户{get;set;}
公共字节状态{get;set;}
}
公开课名称
{
公共int TitleId{get;set;}
公共字符串名称{get;set;}
公共字节IsIntegrationData{get;set;}
公共虚拟列表用户{get;set;}
}

我删除了一些字段,例如ManagerDepartmentId,因为应该通过导航属性Department.Manager.DepartmentId来访问这些字段。

尝试以下操作:

  public class User
    {
        public int UserId { get; set; } 
        public string Username { get; set; } 
        public string Email { get; set; } 
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [ForeignKey("Department")]
        public int DepartmentId { get; set; }
        public virtual Department Department {get; set;}
        
        [ForeignKey("Title")]
        public int TitleId { get; set; }
        public virtual Title Title {get;set;}

        [ForeignKey("Manager")]
        public int ManagerUserId { get; set; }
        public virtual User Manager {get; set;}
        
        [InverseProperty("Manager")]
        public virtual List<Department> DepartmentsManaged {get; set;}
    }


    public class Department
    {
        public int DepartmentId { get; set; }
        public string DepartmentCode { get; set; }
        public string Name { get; set; }

        [ForeignKey("Manager")]
        public int ManagerUserId { get; set; }
        public virtual User Manager {get; set;}

        public virtual List<User> Users {get; set;}
    }


    public class Position
    {
        public int PositionId { get; set; }
        public string PositionCode { get; set; }
        public string Name { get; set; }

        [Required]
        [ForeignKey("User")]
        public int UserId { get; set; }
        public virtual User User {get; set;}

        public byte Status { get; set; }
    }
    public class Title
    {
        public int TitleId { get; set; }
        public string Name { get; set; }
        public byte IsIntegrationData { get; set; }

        public virtual List<User> Users {get; set;}
    }
公共类用户
{
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
[外键(“部门”)]
public int DepartmentId{get;set;}
公共虚拟部门部门{get;set;}
[外键(“标题”)]
公共int TitleId{get;set;}
公共虚拟标题{get;set;}
[外籍钥匙(“经理”)]
public int ManagerUserId{get;set;}
公共虚拟用户管理器{get;set;}
[反向财产(“经理”)]
公共虚拟列表部门托管{get;set;}
}
公共课系
{
public int DepartmentId{get;set;}
公共字符串部门代码{get;set;}
公共字符串名称{get;set;}
[外籍钥匙(“经理”)]
public int ManagerUserId{get;set;}
公共虚拟用户管理器{get;set;}
公共虚拟列表用户{get;set;}
}
公共阶级地位
{
public int PositionId{get;set;}
公共字符串位置代码{get;set;}
公共字符串名称{get;set;}
[必需]
[外键(“用户”)]
public int UserId{get;set;}
公共虚拟用户用户{get;set;}
公共字节状态{get;set;}
}
公开课名称
{
公共int TitleId{get;set;}
公共字符串名称{get;set;}
公共字节IsIntegrationData{get;set;}
公共虚拟列表用户{get;set;}
}
我删除了一些字段,如ManagerDepartmentId,因为应该通过导航属性Department.Manager.DepartmentId访问这些字段