简体   繁体   English

此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)

[英]Only primitive types ('such as Int32, String, and Guid') are supported in this context

I'm getting the following error:我收到以下错误:

Unable to create a constant value of type 'Phoenix.Intranet.Web.ClientSettings.ComponentRole'.无法创建类型为“Phoenix.Intranet.Web.ClientSettings.ComponentRole”的常量值。 Only primitive types ('such as Int32, String, and Guid') are supported in this context.此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。

I understand why the error occurs.我明白为什么会发生错误。 What I don't understand is why my code is creating the error.我不明白为什么我的代码会产生错误。 My comparisons are against primitive types.我的比较是针对原始类型的。 All the comparisons are Guid to Guid.所有比较都是Guid to Guid。 The error specifically states that Guids are ok.该错误特别指出Guids没问题。

The error occurs on this line (towards the bottom):错误发生在这一行(朝向底部):

 var vla =  (from cir in phoenixEntities.ComponentInRoles

Code:代码:

List<ComponentRole> roles;
using (IMSMembershipEntities entities = new IMSMembershipEntities())
{
    roles = (from role1 in entities.Roles
             select new ComponentRole{Name = role1.RoleName, RoleId = role1.RoleId} ).ToList();
}

List<Components> componentInRoles;

using (PhoenixEntities phoenixEntities = new PhoenixEntities())
{
    phoenixEntities.ContextOptions.LazyLoadingEnabled = false;
    componentInRoles = (from component in phoenixEntities.Components
                        select new Components{Name = component.Name,
                                              ComponentId = component.ComponentId,
                                              //InRoles = (from componentInRole in phoenixEntities.ComponentInRoles
                                              //           join role in roles on componentInRole.RoleId equals role.RoleId 
                                              //           where componentInRole.ComponentId == component.ComponentId
                                              //           select new ComponentRole{RoleId = role.RoleId, Name = role.Name})
                                              }
                       ).ToList();


    foreach (Components cmpent in componentInRoles)
    {
        Components cmpent1 = cmpent;
        //cmpent.InRoles = 

         var vla =  (from cir in phoenixEntities.ComponentInRoles
                     join role in roles on cir.RoleId equals role.RoleId
                     where cir.ComponentId == cmpent1.ComponentId
                         select role).ToList();
    }
}

EntityFramework and Linq to SQL both try to translate such queries which a part is in memory and the other part is in Database, to sql IN operator. EntityFramework and Linq to SQL both try to translate such queries which a part is in memory and the other part is in Database, to sql IN operator.

and because your class which roles is an IEnumerable of is not a primitive type it cannot be translated to SQL query.并且因为您的角色是 IEnumerable 的 class 不是原始类型,所以它不能转换为 SQL 查询。

you should first fetch from database to memory and then join two lists in memory.您应该首先从数据库中获取 memory,然后加入 memory 中的两个列表。

for example:例如:

 var vla =  (from cir in phoenixEntities.ComponentInRoles.ToList()
                     join role in roles on cir.RoleId equals role.RoleId
                     where cir.ComponentId == cmpent1.ComponentId
                         select role).ToList();

I hope I helped我希望我有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示无法创建类型“”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context Linq,array.Contains()生成异常:在此上下文中仅支持基本类型(例如Int32,String和Guid&#39;) - Linq, array.Contains() generating exception: Only primitive types ('such as Int32, String, and Guid') are supported in this context EF-Code First:无法创建类型&#39;&#39;的常量值。 在此上下文中仅支持原始类型(例如Int32,String和Guid&#39;) - EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context 无法创建类型为“结算类型”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context 表达式-无法创建类型为&#39;System.Collections.Generic.List`1&#39;的常量值。 仅原始类型(例如Int32,String和Gu - Expression - Unable to create a constant value of type 'System.Collections.Generic.List`1'. Only primitive types ('such as Int32, String, and Gu “在这种情况下,仅支持原始类型” - “Only primitive types are supported in this context” 在这种情况下仅支持基本类型 - Only primitive types are supported in this context 在此上下文中仅支持原始类型或枚举类型 - Only primitive types or enumeration types are supported in this context 在此上下文中仅支持原始类型或枚举类型 - Only primitive types or enumeration types are supported in this context Guid 转换为 int32 - Guid converted to int32
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM