LINQ-TO-SQL Refresh () does not update object associations after some external database update such as ExecuteCommand ()

Suppose I have an object called Department that I got from MyDataContext. The department has an association called Employees.

I want to add a bunch of new employees to the department. Instead of using LINQ, which will lead to multiple queries, I do one bulk insert with:

MyDataContext.ExcecuteCommand(
"Insert INTO Employee (DeptID, etc…), (DeptID, etc…), (DeptID, etc…)"
);

Now my department object does not know anything about these changes, in particular the Employees property, because Department.Employees.Count () returns 0

I tried both of the following: upgrade to update the Department and related employees:

MyDataContext.Refresh(RefreshMode.OverwriteCurrentValues, Department);
MyDataContext.Refresh(RefreshMode.OverwriteCurrentValues, Department.Employees);

But still Department.Employees.Count () returns 0

, , , DataContext , Department.Employees.Count() = 3

MyDataContext DataContext?

( DataContext , DataContext)

+5
1

, , Refresh() . :

Department.Employees.Clear();
Department.Employees.AddRange(MyContext.Employees.Where(x=>x.DeptID=Department.DeptID))
+5

All Articles