【问题标题】:Relational Database Mapping in MVCMVC 中的关系数据库映射
【发布时间】:2016-06-12 05:26:33
【问题描述】:

我为代码量道歉,但我只需要确认我的映射和 [ForeignKey("XXX") 的使用是否正确。

在像 Department 和 Depot 这样的类中,我需要 public int DepartmentID { get; set; } 行还是在将数据插入数据库时​​它们会自动编号。 (DepartmentID = 1(人力资源),DepotID = 2(洛杉矶)

这是我的实体图。

用户.cs

public class User
{

    public int UserID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    [ForeignKey("AdministratorID")]
    public virtual Administrator Administrator { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }


    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }

    public int TicketID { get; set; }
    //Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
    public virtual ICollection<Ticket> Users { get; set; }

}

Ticket.cs

public class Ticket
{
    public string Issue { get; set; } 
    [DisplayFormat(NullDisplayText = "No Priority")]
    public Priority? Priority { get; set; }
    //Category One to Many Ticket
    public int CategoryID { get; set; }
    [ForeignKey("CategoryID")]
    public virtual Category Category { get; set; }
    //User (One to Many) Ticket
    public int UserID { get; set; }
    public int TicketID { get; set; }
    [ForeignKey("TicketID")]
    public virtual User User { get; set; }
    public int AdminID { get; set; }
    public virtual ICollection<Administrator> Administrators { get; set; }

}

仓库.cs

public class Depot
{
    public int DepotID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

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

}

Department.cs

public class Department
{
    public int DepartmentID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

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

Category.cs

public class Category
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CategoryID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Ticket> Tickets { get; set; }
}

Administrator.cs

    public class Administrator
{
    [Key, ForeignKey("User")]
    public int UserID { get; set; }
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]
    public string AdminRole { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc mapping relational-database


    【解决方案1】:

    ID 属性将成为该类对应的数据库表的主键列。默认情况下,实体框架将名为 ID 或 classnameID 的属性解释为主键。

    Depot 和 User 关系见下文(注意用户中的 DepotID 和 Depot 是如何标记为虚拟以启用延迟加载的)

    public class Depot
    {      
        public int DepotID { get; set; }
    
        [StringLength(50, MinimumLength = 3)]
        public string Name { get; set; }
    
        public virtual ICollection<User> Users { get; set; }
    
    }
    
    public class User
    {
    
        public int UserID { get; set; }
        [StringLength(50, MinimumLength = 1)]
        public string LastName { get; set; }
        [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]
    
        [Column("FirstName")]
        public string FirstMidName { get; set; }
    
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EnrollmentDate { get; set; }
    
        public string FullName
        {
            get { return LastName + ", " + FirstMidName; }
        }
        public int AdministratorID { get; set; }
        public virtual Administrator Administrator { get; set; }
    
        public int DepartmentID { get; set; }
        public virtual Department Department { get; set; }
    
        public virtual int DepotID { get; set; }
    
        [ForeignKey("DepotID")]
        public virtual Depot Depot { get; set; }
    
        public int TicketID { get; set; }
        public virtual ICollection<Ticket> Users { get; set; }
    
    }
    

    【讨论】:

    • 所以在 Depot 类中有一个主键(DepotID)和一个外键(UserID)是可以的
    • Depot 类不需要 UserID,因为 Depot 是 User 类的外键(根据您的实体图)。 Depot 实体的 Users 属性将包含与该 Depot 实体相关的所有 User 实体。换句话说,如果数据库中给定的 Depot 行有两个相关的 User 行(在其 DepotID 外键列中包含该 Depot 的主键值的行),则该 Depot 实体的 Users 导航属性将包含这两个 User 实体。我会相应地更新我的答案
    • 感谢您的详细解释。一对一关系或多对多关系怎么样。示例:管理员和用户,因为只有一个用户可以拥有一个管理员角色(检查用户类和管理员类)。注意:我使用您的解决方案重新编辑了我的代码,您可以检查我是否正确应用了您所说的内容!谢谢
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多