I'm using the Code First approach and have the following Model:
public class Person
{
public int ID {get; set;}
public string Name {get; set;}
public int CurrentStationID {get; set;}
public virtual Station CurrentStation {get; set;}
public int CurrentTransportationID {get; set;}
public virtual Transportation CurrentTransporation {get; set;}
}
And the following code in my controller:
Models.Person model = myDBContext.Persons
.Include("CurrentStation")
.Include("CurrentTransportation")
.Where(p => p.ID == 1)
.FirstOrDefault();
"model" is going to be NULL even though a row with (1, "Testname", 0, 0) exists in the DB table "persons".
The above generates a SQL-statement with [...] INNER JOIN stations AS [...] LEFT JOIN transportations [...]
. It always does an INNER JOIN for the first navigational property, for all my models, regardless what the underlying table/type is or in which order I specify them, it's always the first Include().
I do not want an INNER JOIN but a LEFT JOIN on all of them, since "CurrentStation" is not required to be in the database.
Why does EF do this??? I don't understand but I want to understand. I have not specified a [Required]-attribute on any of the properties, yet it behaves as if I did.
Unfortunately there's no [Optional]-attribute and doing a "HasOptional()" via the OnModelCreate-override is not an option for me.