I'm attempting to use navigation properties for the first time in lieu of doing joins. I cannot get a situation to work where it would normally be done with a left outer join. In the following example, I'm only getting results back where the foreign key is non-null. I need all results back. What am I missing?
public class User
{
[Key]
public int UserID {get;set;}
public String Name {get;set;}
}
public class Shipment
{
[Key]
public int ShipmentID {get;set;}
public int? SignedForByID {get;set;}
[ForeignKey("SignedForByID")]
public virtual User SignedForBy{get;set;}
}
navigation property mapping:
Shipment.HasOptional(x=> x.SignedForBy).WithMany()
.HasForeignKey(y=> y.SignedForByID).WillCascadeOnDelete(false);
query:
var data = (from s in context.Set<Shipment>()
select new {
ShipmentID = s.ShipmentID,
SignedForBy = s.SignedForBy
});
The problem was not shown in this example. The mappings are generated by a helper class. In some cases, the helper class incorrectly mapped the navigation properties with the .HasRequired() method instead of the .HasOptional(). Correcting the mapping class has fixed the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With