2011-06-30 31 views
10

Tôi nhận được lỗi sau:loại Chỉ nguyên thủy ('như Int32, String, và Guid') được hỗ trợ trong bối cảnh này

Unable to create a constant value of type 'Phoenix.Intranet.Web.ClientSettings.ComponentRole'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Tôi hiểu tại sao lỗi xảy ra. Những gì tôi không hiểu là tại sao mã của tôi đang tạo ra lỗi. So sánh của tôi là chống lại các loại nguyên thủy. Tất cả các so sánh là Guid to Guid. Lỗi cụ thể nói rằng Guids là ok.

Lỗi này xảy ra trên đường dây này (hướng về phía dưới):

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(); 
    } 
} 
+0

Có một mối quan hệ giữa Foels và phoenixEntities? – Jethro

Trả lời

18

EntityFramework và LINQ to SQL cả cố gắng dịch truy vấn như vậy mà một phần là ở bộ nhớ và phần còn lại nằm trong Database, cho toán tử sql IN.

và vì lớp của bạn mà vai trò là một IEnumerable không phải là loại nguyên thủy, nó không thể dịch sang truy vấn SQL.

trước tiên bạn nên tìm nạp từ cơ sở dữ liệu vào bộ nhớ và sau đó tham gia hai danh sách trong bộ nhớ.

ví dụ:

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(); 

Tôi hy vọng tôi đã giúp

+0

ahh, tôi hiểu, về cơ bản tôi đang trộn LINQ-to-Objects và LINQ-to-Entities. –

+0

Điều đó đã sửa nó. Cảm ơn! –

Các vấn đề liên quan