本文介绍了使用EF4.1查询时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个关系,有多对多关系,但只有一方具有可见性
所以我把这行放在dbcontext classe中以获得多对多
modelBuilder.Entity< G >()。HasMany(t => ; tC).WithMany();
然后我有了这个查询,我收到了gvariable作为参数,两个类G和C都有一个很长的Id属性设置为具有自动同一性的密钥,有树表,每个实体一个,多对多关系,存储库扩展IRepository
接口,All方法返回DbSet
crep.All()。其中(c => gvariable.C.Any(a => c.Id == a。 Id))
此查询返回此错误
无法创建类型为"C"的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')。
描述:
执行当前网页时发生未处理的异常请求。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息: System.NotSupportedException:无法创建类型为"C"的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')。
来源错误:
第53行:< tr class = QUOT; tabelaSemCSS"> 第54行:< td> 第55行:<%= Html.ListBox(" Disponiveis",Model.Disponiveis.ToSelectItems(),new {@size =" 10",@ style =" width:325px",@ name =" Disponiveis",@ id =" Disponiveis",ondblclick =" adicionar();"})%> 第56行:< / td> 第57行:< td width =" 50px"> |
源文件: c:\ Projetos \Cebrace \ SOACCDBPMSA \Cebrace-SOAC-PMSA-CDB \\ \\ CebraceWeb \Areas\Cadastros \Views \Shared \SharedAgrupamento.ascx 行:
55
堆栈跟踪:
[NotSupportedException:无法创建类型为C的常量值"。在此上下文中仅支持原始类型(例如Int32,String和Guid'。) System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent,ConstantExpression linq)+862 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq)+54 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)+110 System.Data .Objects.ELinq。<> c__DisplayClass77。< TypedTranslate> b__75(表达式e)+11 System.Linq.WhereSelectEnumerableIterator`2.MoveNext()+ 151 System.Data.Common.CommandTrees .ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1参数,String argumentName,Int32 expectedElementCount,Boolean allowEmpty,Func`3 map,Func`2 collect,Func`3 deriveName)+237 System.Data。 Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate()+46 System.Data.Common.CommandTrees.ExpressionBuilder.Int ernal.ArgumentValidation.CreateExpressionList(IEnumerable`1 arguments,String argumentName,Boolean allowEmpty,Action`2 validationCallback)+142 System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateNewCollection(IEnumerable`1 elements,DbExpressionList& ; validElements)+72 System.Data.Objects.ELinq.NewArrayInitTranslator.TypedTranslate(ExpressionConverter parent,NewArrayExpression linq)+121 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq)+54 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)+110 System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent,ConstantExpression linq)+520 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq)+54 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)+110 System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,DbExpression& source,DbExpressionBinding& sourceBinding,DbExpression& lambda)+49 System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent ,MethodCallEx压力调用)+37 System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,SequenceMethod sequenceMethod)+14 System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent ,MethodCallExpression linq)+102 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq)+54 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq )+110 System.Data.Objects.ELinq.NotTranslator.TypedTranslate(ExpressionConverter parent,UnaryExpression linq)+30 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq) )+54 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)+110 System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression input)+80 System.Data.Objects.ELinq.ExpressionConvert er.TranslateLambda(LambdaExpression lambda,DbExpression输入,DbExpressionBinding&绑定)+88 System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,DbExpression& source,DbExpressionBinding& sourceBinding,DbExpression& lambda)+85 System.Data.Objects。 ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call)+37 System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,SequenceMethod sequenceMethod)+14 System.Data.Objects .ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq)+102 System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq)+54 System.Data.Objects .ELinq.ExpressionConverter.TranslateExpression(Expression linq)+110 System.Data.Objects.ELinq.ExpressionConverter.Convert()+16 System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +110 System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)+149 System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable< T>。 GetEnumerator()+44 System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()+ 40 System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic。 IEnumerable< TResult> .GetEnumerator()+ 40 System.Linq.WhereSelectEnumerableIterator`2.MoveNext()+63 System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)+88 System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection,IEqualityComparer`1 comparer)+125 System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper,String optionLabel ,String name,IEnumerable`1 selectList,Boolean allowMultiple,IDictionary`2 htmlAttributes)+449 System.Web.Mvc.Html.SelectExtensions.ListBox(HtmlHelper htmlHelper,String name,IEnumerable`1 selectList,Object htmlAttributes)+ 32 ASP.areas_cadastros_views_shared_sharedagrupamento_ascx .__ Render__control1(HtmlTextWriter __w,Control parameterContainer)位于c:\Projetos \Cebrace \ SOACCDBPMSA \Cebrace-SOAC-PMSA-CDB \CebraceWeb \Areas\Cadastros \Views \Shared \ SharedAgrupamento.ascx:55 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+109 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)+8 System.Web.UI.Control.Render(HtmlTextWriter writer)+10 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)+27 System.Web.UI.Control.RenderControl (HtmlTextWriter编写器,ControlAdapter适配器)+100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+208 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)+8 System.Web.UI.Page.R ender(HtmlTextWriter writer)+29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)+43 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter adapter)+100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Page .ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3060 |
为什么会收到此错误?
为什么无法使用crep.All()。其中(c ) ; => gvariable.C.Contains(c))?
解决方案
I have two classes with a relation many to many but with only one side with visibility
so I put this line in the dbcontext classe to get the many to many
modelBuilder.Entity<G>().HasMany(t => t.C).WithMany();
then I have this query, I received gvariable as parameter, both classes G an C have an long Id property set as key with autoidentity, there are tree tables, one for each entity and one for the many to many relation, and the repository extends an IRepository interface and the All Method returns the DbSet
crep.All().Where(c => gvariable.C.Any(a => c.Id == a.Id))
this query returns this error
Unable to create a constant value of type 'C'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: Unable to create a constant value of type 'C'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Source Error:
Line 53: <tr class="tabelaSemCSS">
Line 54: <td>
Line 55: <%= Html.ListBox("Disponiveis", Model.Disponiveis.ToSelectItems(), new { @size = "10", @style = "width:325px", @name = "Disponiveis", @id = "Disponiveis", ondblclick = "adicionar();" })%>
Line 56: </td>
Line 57: <td width="50px">
|
Source File: c:\Projetos\Cebrace\SOACCDBPMSA\Cebrace-SOAC-PMSA-CDB\CebraceWeb\Areas\Cadastros\Views\Shared\SharedAgrupamento.ascx Line:55
Stack Trace:
[NotSupportedException: Unable to create a constant value of type 'C'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.]
System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) +862
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.<>c__DisplayClass77.<TypedTranslate>b__75(Expression e) +11
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1 argument, String argumentName, Int32 expectedElementCount, Boolean allowEmpty, Func`3 map, Func`2 collect, Func`3 deriveName) +237
System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate() +46
System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.CreateExpressionList(IEnumerable`1 arguments, String argumentName, Boolean allowEmpty, Action`2 validationCallback) +142
System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateNewCollection(IEnumerable`1 elements, DbExpressionList& validElements) +72
System.Data.Objects.ELinq.NewArrayInitTranslator.TypedTranslate(ExpressionConverter parent, NewArrayExpression linq) +121
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) +520
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) +49
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +37
System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +14
System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +102
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.NotTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq) +30
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) +80
System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding) +88
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) +85
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +37
System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +14
System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +102
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
System.Data.Objects.ELinq.ExpressionConverter.Convert() +16
System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +110
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +149
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44
System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() +40
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +63
System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other) +88
System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer) +125
System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +449
System.Web.Mvc.Html.SelectExtensions.ListBox(HtmlHelper htmlHelper, String name, IEnumerable`1 selectList, Object htmlAttributes) +32
ASP.areas_cadastros_views_shared_sharedagrupamento_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Projetos\Cebrace\SOACCDBPMSA\Cebrace-SOAC-PMSA-CDB\CebraceWeb\Areas\Cadastros\Views\Shared\SharedAgrupamento.ascx:55
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +43
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060
|
Why do I get this error?
Why is not possible to use crep.All().Where(c => gvariable.C.Contains(c))?
解决方案
这篇关于使用EF4.1查询时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!