![](/img/trans.png)
[英]Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context
[英]Only primitive types ('such as Int32, String, and Guid') are supported in this context
我收到以下錯誤:
無法創建類型為“Phoenix.Intranet.Web.ClientSettings.ComponentRole”的常量值。 此上下文僅支持原始類型(“例如 Int32、String 和 Guid”)。
我明白為什么會發生錯誤。 我不明白為什么我的代碼會產生錯誤。 我的比較是針對原始類型的。 所有比較都是Guid to Guid。 該錯誤特別指出Guids沒問題。
錯誤發生在這一行(朝向底部):
var vla = (from cir in phoenixEntities.ComponentInRoles
代碼:
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.
並且因為您的角色是 IEnumerable 的 class 不是原始類型,所以它不能轉換為 SQL 查詢。
您應該首先從數據庫中獲取 memory,然后加入 memory 中的兩個列表。
例如:
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();
我希望我有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.