【发布时间】:2019-12-03 06:00:54
【问题描述】:
我有一个 Employee 模型:
public class Employee
{
public int Id { get; set; }
[Required]
[StringLength(30, MinimumLength = 5, ErrorMessage = "Name must be between 5 and 30 characters.")]
[RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")]
public string Name { get; set; }
[Required]
[Display(Name = "Direct Contact")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid contact number. Must be 10 digits.")]
public string Phone { get; set; }
[Display(Name = "Personal Cell")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid contact number. Must be 10 digits.")]
public string Phone2 { get; set; }
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid fax number. Must be 10 digits.")]
public string Fax { get; set; }
[Required]
public string Email { get; set; }
[Display(Name = "Personal Email")]
public string Email2 { get; set; }
}
可以在 /employees/ 的表中查看员工数据
然后我正在尝试创建一个项目模型(可以在 /projects/ 中查看)。基本上,在查看项目列表时,其表中的类别之一将是项目联系人。因此,我希望能够将其中一名员工姓名链接为项目联系人。
这是我的项目模型:
public class Project
{
public int Id { get; set; }
[ForeignKey("Employee")]
public int EmployeeId { get; set; }
public string ProjectName { get; set; }
[DataType(DataType.Date)]
public DateTime DueDate { get; set; }
public virtual Employee Employee { get; set; }
}
我这样做的方式是否正确?我似乎无法弄清楚如何正确链接这两个表,以便我可以从项目视图中访问员工的姓名。
- 编辑 -
我在想我拥有它的方式,当将数据加载到 Project 表中时,我可以将 EmployeeID 添加为 Employee 表中的任何员工 ID。然后我可以正确访问它吗?我这样想对吗?
【问题讨论】:
标签: .net asp.net-mvc entity-framework asp.net-core model-view-controller