【问题标题】:Error Message: "Only primitive types or enumeration types are supported in this context."错误消息:“在此上下文中仅支持原始类型或枚举类型。”
【发布时间】:2013-05-26 01:10:39
【问题描述】:

我正在为 asp.net 页面编写报告,我正在尝试将此 linq 语句的结果分配给现有数据集,但我收到“在此上下文中仅支持原始类型或枚举类型。”错误信息。这是我的代码:

   var contextTable = new dbpersonnelEntities();                
   var contextHistory = new WriterInResidenceEntities();

   var rslt = (from c in contextTable.TableofOrganizationRpts
            where !(from o in contextHistory.emp_history
            select o.employeeID).Contains((int)c.employeeid_fk)
                       select c).ToList();

   ReportDataSource r = new ReportDataSource("tableOfOrgDS", rslt);

【问题讨论】:

  • dbpersonnelEntitiesWriterInResidenceEntities是两种不同的类型,派生自ObjectContext(或DbContext)吗?
  • @Dennis 是的,它们是源自两个不同实体模型的两种不同类型。我试图简单地列出 TableofOrganizationRpts 表中不存在于 emp_history 表中的所有员工。如果我尝试在代码中使用单个 objectcontext,它会起作用,但是当我尝试执行此连接时,我会收到错误。

标签: c# asp.net sql linq entity-framework


【解决方案1】:

您不能在单个 LINQ 查询中混合来自不同上下文的实体类型。要么将它们放入单个上下文中,要么尝试执行此操作:

var employeeIds = (from o in contextHistory.emp_history select o.employeeID).ToList();

然后将此列表传递给第二个查询:

var rslt = (from c in contextTable.TableofOrganizationRpts
            where !employeeIds.Contains((int)c.employeeid_fk)
            select c).ToList();

这应该在生成的 SQL 查询中生成 IN 条件。请注意,这可能运行缓慢。

【讨论】:

  • 这工作完美!这是一个相当短的列表,因此几乎没有任何延迟。非常感谢!!我是实体框架的新手,现在我将知道如何正确处理多个实体类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多