1
外部キー値がnullの可能性があるエンティティのリストを取得できません。Entity Framework null 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
にNULL値が、それは、リスト内のContact
は、それが返されることをスキップしますしているすべてのContacts
そのうちの一つのリストを取得。たとえば、クエリvar contacts = _context.Contacts.Include(c => c.Company).ToList();
のみ、以下の表からジョス・ストーンを返します。
を私は、Entity Frameworkのコア1.0.0を使用しています。どんな助力も心から感謝します。