One of my Entities is not a proxy

c# entity-framework entity-framework-6

Question

The error I am receiving-

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: The value "PHSRP_Dashboard.WorkSessionBreak" is not of type "System.Data.Entity.DynamicProxies.WorkSessionBreak_06908<..many characters..>6E3E" and cannot be used in this generic collection.

only occurs when a new WorkSessionBreak record has been added to the database. It does not occur for records that have only been modified, nor will it re-occur when the program is restarted and the (not so) new record is revisited.

private void Edit_WorkSessionBreak_FormClosed(object sender, FormClosedEventArgs e)
{
    if (SelectedWorkSessionID >= 0)
    {
        var BreakSet = editEmployee.EmployeeWorkSessions
                                   .SelectMany(ws => ws.WorkSessionBreaks)
                                   .Where(b => b.DELETED != true && b.EmployeeWorkSessionID == SelectedWorkSessionID);

        if (BreakSet.Any())
        {
            var BreakSetSorted = BreakSet.OrderBy(b => b.OutTime);
            this.bs_WorkSessionBreaks_Display.DataSource = BreakSetSorted;        //  Blows-up on New record added.
        }
        else
        {
            this.bs_WorkSessionBreaks_Display.DataSource = new BindingList<WorkSessionBreak>();
        }
    }
    else
    {
        this.bs_WorkSessionBreaks_Display.DataSource = new BindingList<WorkSessionBreak>();
    }

    if (bs_WorkSessionBreaks_Display.Count > 0)
    {
        if (syncToViewBreak(((WorkSessionBreak)bs_WorkSessionBreaks_Display.Current).WorkSessionBreakID))
        {
            SelectedWorkSessionBreakID = ((WorkSessionBreak)bs_WorkSessionBreaks_Display.Current).WorkSessionBreakID;
        }
    }

    bs_WorkSessionBreaks_Display.ResetBindings(false); 
}

The Dialog form we returned from is working correctly. The changes/additions have been correctly placed in the database, but the BindingSources associated with this Parent form are not automatically reordering.

An examination of the variable contents in the debugger shows that the New record in the Enumerable returned by LINQ is a WorkSessionBreak while the other pre-existing items are WorkSessionBreak Proxies.

Debugger View 1

The Sorting works but also returns a mix of Types and Proxies:

Debugger View 2

The next instruction crashes because one item is not the same type (i.e. Proxy) as the others.

Debugger View 3

I have no idea what to do about this or why it is occurring. This algorithm is used in other parts of this same form without this error occurring.

1
0
8/1/2018 1:27:36 AM

Accepted Answer

What you are facing is what is known as deferred execution. Basically, the code is attempting to add items to a database query.

Here:

var BreakSet = editEmployee.EmployeeWorkSessions
    .SelectMany(ws => ws.WorkSessionBreaks)
    .Where(b => b.DELETED != true 
             && b.EmployeeWorkSessionID == SelectedWorkSessionID);

BreakSet is an IQueryable<WorkSessionBreak>. Whenever you see an IQueryable<SomeClass>, remember that it represents a query and (normally) not data you have in memory.

var BreakSet = editEmployee.EmployeeWorkSessions
   .SelectMany(ws => ws.WorkSessionBreaks)
   .Where(b => b.DELETED != true 
            && b.EmployeeWorkSessionID == SelectedWorkSessionID)
   .OrderBy(b => b.OutTime)
   .ToList();

if (BreakSet.Any())
{
    this.bs_WorkSessionBreaks_Display.DataSource = BreakSet;
}

With this, you now have a single database call. The OrderBy clause doesn't affect the results of a COUNT query, so you can make it as part of the initial query. Notice that the ToList() call at the end is what executes the query so the results are materialized.

1
8/1/2018 1:38:14 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