Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的一个实体不是代理_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 我的一个实体不是代理

C# 我的一个实体不是代理,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我收到的错误- 中发生类型为“System.ArgumentException”的未处理异常 mscorlib.dll 其他信息:值PHSRP_Dashboard.WorkSessionBreak 他不是那种人 System.Data.Entity.DynamicProxies.WorkSessionBreak_069086E3E,不能在此常规集合中使用 仅当新的WorksSessionBreak记录添加到数据库时发生。它不会发生在只修改过的记录上,也不会在程序重新启动和重新访问不太新的记录时再

我收到的错误-

中发生类型为“System.ArgumentException”的未处理异常 mscorlib.dll

其他信息:值PHSRP_Dashboard.WorkSessionBreak 他不是那种人 System.Data.Entity.DynamicProxies.WorkSessionBreak_069086E3E,不能在此常规集合中使用

仅当新的WorksSessionBreak记录添加到数据库时发生。它不会发生在只修改过的记录上,也不会在程序重新启动和重新访问不太新的记录时再次发生

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); 
}
我们从中返回的对话框窗体工作正常。更改/添加已正确放置在数据库中,但与此父窗体关联的BindingSources不会自动重新排序

对调试器中变量内容的检查表明,LINQ返回的枚举中的新记录是WorkSessionBreak,而其他预先存在的项是WorkSessionBreak代理

排序起作用,但也返回类型和代理的混合:

下一条指令崩溃,因为一个项与其他项的类型不同,即代理


我不知道该怎么办,也不知道为什么会这样。此算法在同一表单的其他部分中使用,不会发生此错误。

您所面临的就是所谓的。基本上,代码试图向数据库查询添加项

在这里:

BreakSet是一个IQueryable。每当看到IQueryable时,请记住它代表一个查询,而通常不是内存中的数据

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;
}
有了它,您现在就有了一个数据库调用。OrderBy子句不会影响COUNT查询的结果,因此可以将其作为初始查询的一部分。请注意,最后的ToList调用是执行查询以便将结果具体化的调用

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;
}