简体   繁体   English

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

[英]I have 4 class and class properties, but how should I relate the classes? (one-one, one-many …)

which table should be associated with which table?哪个表应该与哪个表关联?

How do I add foreign keys?如何添加外键?

I am confused.我很困惑。 Can you help me?你能帮助我吗?

ex: user and department ---> one to many?例如:用户和部门 ---> 一对多?

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

Try something like this:尝试这样的事情:

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

I removed a few fields such as ManagerDepartmentId because this should be accessed through the navigation property Department.Manager.DepartmentId.我删除了一些字段,例如 ManagerDepartmentId,因为这应该通过导航属性 Department.Manager.DepartmentId 访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM