c# - MVC 中的关系数据库映射

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

对于代码量,我深表歉意,但我只需要对 [ForeignKey("XXX") 的映射和使用进行一些确认。正确与否。

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

这是我的实体图。 enter image description here

用户.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; }

}

票务.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; }

}

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

}

部门.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; }
}

类别.cs

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

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

管理员.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; }
}

最佳答案

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

请参阅下面的 Depot 和 User 关系(注意 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; }

}

关于c# - MVC 中的关系数据库映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695056/

相关文章:

javascript - 脚本运行失控

elasticsearch - 根映射定义具有不受支持的参数:[_timestamp:{enabled = true}]

c# - 在 C# 中无限旋转列表中的 3 个项目的最佳方法是什么

c# - 当无法找到请求的 PostgreSQL 表时,如何捕获异常并使用错误代码?

c# - 字符串格式异常

c# - 使用中的错误案例列表 _userManager.CreateAsync(user, password)

asp.net - Kestrel 和 Katana 之间的区别

Python:如何将数字映射到列中的唯一项目(枚举唯一对象)?

Haskell - 以不同方式映射奇数放置值和偶数放置值

c# - 无法在 Win8.1 C# 应用程序中将应用程序锁定为横向