Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core nullable foreign key

I am having trouble getting a list of entities where a foreign key value could be null.

The Models:

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; }
}

When I try and get the list of all Contacts and one of them has a null value for CompanyId in my database, it will skip that Contact in the list it returns. For example, the query var contacts = _context.Contacts.Include(c => c.Company).ToList(); only returns Josh Stone from the following table:

Contacts Table

I am using Entity Framework Core 1.0.0. Any help would be sincerely appreciated.

like image 921
Christopher Drzal Avatar asked Oct 27 '16 18:10

Christopher Drzal


People also ask

Can you have a nullable foreign key?

A table can have many foreign keys. A foreign key is nullable if any part is nullable. A foreign key value is null if any part is null.

How do you make a foreign key nullable in C#?

A foreign key is NULLABLE by DEFAULT in code first asp.net mvc - 5 entity framework. If we want to make it non nullable. we need to either use fluent api if not then decorate with "Required" attribute.

How do I set a foreign key in EF core?

If you want the foreign key to reference a property other than the primary key, you can use the Fluent API to configure the principal key property for the relationship. The property that you configure as the principal key will automatically be set up as an alternate key.

How do you set a foreign key in an entity?

The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.


1 Answers

Supposing that it did actually return mary, what would you expect her CompanyId to be? I would suspect null (what else could it be?), in which case your model is wrong and public int CompanyId { get; set; } should be public int? CompanyId { get; set; }

like image 74
Robert McKee Avatar answered Sep 23 '22 06:09

Robert McKee