Entity framework not showing values for a specific column in query result

c# entity-framework-6 sql sql-server winforms

Question

I have a SQL Server database mapped in Entity Framework 6.

Database Model in EF enter image description here

The problem is that when I query the Employee table for an employee or for all employees, it doesn't return the Equipments mapped with the particular employee. The Equipments column is empty.

The results are being displayed in a DataGridView

Query Result for a Particular Employee enter image description here

I queried like this:

SELECT * 
FROM Employee 
WHERE EmployeeID = 5;

What seems to be the problem? Can anyone help me out here?

1
0
1/16/2019 10:39:10 AM

Popular Answer

Here are a few examples in Linq that would match your query but also include the equipment.

using (var context = new EmployeeContext()) {
    var employeeData = context.Employees.Where(p=> p.EmployeeID == 5)
                       .Include(t => t.Equipment).FirstOrDefault();
}

or

using (var context = new EmployeeContext()) {
    var employeeData = context.Employees.SqlQuery("SELECT * FROM Employee E JOIN Equipment EE ON E.EmployeeID = EE.EmployeeID WHERE E.EmployeeID = @id", new SqlParameter("@id", 5)).FirstOrDefault();
    }

I would suggest reading how to pass the parameters here

Entity Framework Raw SQL Queries

Execute Raw SQL Queries in Entity Framework 6:

0
3/27/2018 5:21:34 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow