Entity Framework - “Unable to create a constant value of type 'Closure type'…” error

前端 未结 4 990
感情败类
感情败类 2020-11-27 19:07

Why do I get the error:

Unable to create a constant value of type \'Closure type\'. Only primitive types (for instance Int32, String and Guid) are s

4条回答
  •  情话喂你
    2020-11-27 19:54

    I have found the cause of the error (I am using Framework 4.5). The problem is, that EF a complex type, that is passed in the "Contains"-parameter, can not translate into an SQL query. EF can use in a SQL query only simple types such as int, string...

    this.GetAll().Where(p => !assignedFunctions.Contains(p))
    

    GetAll provides a list of objects with a complex type (for example: "Function"). So therefore, I would try here to receive an instance of this complex type in my SQL query, which naturally can not work!

    If I can extract from my list, parameters which are suited to my search, I can use:

    var idList = assignedFunctions.Select(f => f.FunctionId);
    this.GetAll().Where(p => !idList.Contains(p.FunktionId))
    

    Now EF no longer has the complex type "Function" to work, but eg with a simple type (long). And that works fine!

提交回复
热议问题