我有困难获取一个外键值可能为null的实体列表。
模型:
当我尝试获取所有联系人列表时,如果其中一个联系人在我的数据库中的
public class Company
{
[Key]
[Required]
public int Id { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
[Display(Name = "Main Phone")]
public string LandPhone { get; set; }
[Display(Name = "Fax")]
public string FaxPhone { get; set; }
}
public class Contact
{
[Key]
[Required]
public int Id { get; set; }
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[EmailAddress]
public string Email { get; set; }
[Display(Name = "Mobile Phone")]
public string MobilePhone { get; set; }
[Display(Name = "Office Phone")]
public string LandPhone { get; set; }
[Display(Name = "Fax")]
public string FaxPhone { get; set; }
public string Title { get; set; }
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
public Company Company { get; set; }
}
当我尝试获取所有联系人列表时,如果其中一个联系人在我的数据库中的
CompanyId
列中具有空值,则它将跳过该联系人并不会返回其信息。例如,以下查询语句:var contacts = _context.Contacts.Include(c => c.Company).ToList();
只返回以下表格中的Josh Stone一行:
我使用的是Entity Framework Core 1.0.0。非常感谢您的帮助。