Reputation: 2883
My table structure looks like :
Account
AccountId
LoginName
EmployeeId - nullable
Employee
FirstName,
SecondName,
etc..
I have query :
var data = from o in _accountRepository.AsQueryableWithIncludes(x => x.Employee, x => x.Permissions)
select new AccountGridVM
{
AccountId = o.AccountId,
EmployeeFirstName = o.EmployeeId == null ? String.Empty: o.Employee.FirstName,
LoginName = o.LoginName,
Permissions = o.Permissions.Select(s => s.NameCZ)
};
return View(new GridModel { Data = data });
My problem is in tenar operator in EmployeeFirstName, entity framework always fetch only accounts, which has employeeId assign, but i need fetch all accounts.
If i remove EmployeeFirstName propeprty, ef fetch all rows.
Where is problem?
Thanks
Upvotes: 2
Views: 350
Reputation: 2883
Problem solved. I had mistake in mapping.
//bad
HasRequired(x => x.Employee).WithMany().HasForeignKey(x => x.EmployeeId); // inner join
//good
HasOptional(x => x.Employee).WithMany().HasForeignKey(x => x.EmployeeId); // left join
Upvotes: 1