【问题标题】:I have 4 class and class properties, but how should I relate the classes? (one-one, one-many ...)我有 4 个类和类属性,但我应该如何关联这些类? (一对一,一对多……)
【发布时间】:2020-07-16 15:32:46
【问题描述】:

哪个表应该与哪个表关联?

如何添加外键?

我很困惑。你能帮帮我吗?

ex : 用户和部门 ---> 一对多?

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

【问题讨论】:

    标签: c# sql entity-framework


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      试试这样的:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-03
        相关资源
        最近更新 更多