0
我哈瓦上,我添加了以下字段的類「員工」:包括未亞型有工作 - EF代碼第一次
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(50)]
public string FirstName { get; set; }
我有兩個其他類「高級」和「少年」上,我已經添加了以下字段:
[Key]
[ForeignKey("Employee")]
public int Id { get; set; }
[Required]
public Employee Employee { get; set; }
「小型」和「高級」各自具有特定的字段(除了在員工中發現的常見字段)。
總之,「高級」和「初級」班與「員工」班共用同一個鑰匙。 在SQL中,Id字段(在「Senior」和「Junior」表中)是主鍵和外鍵。
這個系統工作完美!
我的問題是這樣的:我想能夠從員工的實例訪問初級(例如:員工[0] .Junior)
要做到這一點,我添加了以下字段Employee類:
[ForeignKey("Id")]
public Senior Senior { get; set; }
[ForeignKey("Id")]
public Junior Junior { get; set; }
實體框架,我得到的所有員工是這樣的:
var employees = _context.Employees.Include("Junior").Include("Senior").ToList();
問題是所有初級和高級字段都爲空。 「包含」指令似乎不起作用。
它適合我。 –