Chr*_*zal 7 asp.net-mvc entity-framework-core
我无法获取外键值可能为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; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获取所有列表Contacts并且其中一个CompanyId在我的数据库中具有空值时,它将跳过Contact它返回的列表中的值.例如,查询var contacts = _context.Contacts.Include(c => c.Company).ToList();仅返回下表中的Josh Stone:
我正在使用Entity Framework Core 1.0.0.任何帮助将是真诚的感谢.
Rob*_*Kee 13
假设它确实回归玛丽,你会期望她的CompanyId是什么?我怀疑是null(它还能是什么?),在这种情况下你的模型是错的,
public int CompanyId { get; set; }
应该是
public int? CompanyId { get; set; }
| 归档时间: |
|
| 查看次数: |
10033 次 |
| 最近记录: |