gpt4 book ai didi

c# - Entity Framework - 索引(从零开始)必须大于或等于零

转载 作者:行者123 更新时间:2023-11-30 21:01:16 25 4
gpt4 key购买 nike

我已经犯了一段时间的错误,在阅读她在 SO 上的所有其他帖子时,我并没有得到更多的切肉刀。

进行数据库调用时出现错误:

Index (zero based) must be greater than or equal to zero

我有以下方法给出了错误:

    public bool IsUserInRole(User user, string roleName)
{
using (var repository = new SaxoPublishingEntities())
{
repository.Attach(user);

// THIS CALLS GIVE ME THE ERRROR
var roles = repository.UserRoles.Include("Users").Where(userRole =>
userRole.Name == roleName && userRole.Users.Contains(user)).ToList();


if (roles.Any())
{
return true;
}
return false;
}
}

我的数据库模型:

enter image description here

我的堆栈跟踪:

[FormatException: Indekset (nulbaseret) skal være større end eller lig med nul og mindre end størrelsen af argumentlisten.]
System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +10610374
System.String.Format(IFormatProvider provider, String format, Object[] args) +63
System.Data.EntityRes.GetString(String name, Object[] args) +363
System.Data.Entity.Strings.ELinq_UnsupportedConstant(Object p0) +95
System.Data.Objects.ELinq.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) +1256
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
System.Data.Objects.ELinq.ContainsTranslator.TranslateContains(ExpressionConverter parent, Expression sourceExpression, Expression valueExpression) +136
System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +1011
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
System.Data.Objects.ELinq.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) +122
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) +168
System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding) +161
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) +168
System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +66
System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +47
System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +169
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
System.Data.Objects.ELinq.ExpressionConverter.TranslateSet(Expression linq) +53
System.Data.Objects.ELinq.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +197
System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +47
System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +169
System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +88
System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +162
System.Data.Objects.ELinq.ExpressionConverter.Convert() +50
System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +593
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +255
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +89
System.Linq.Enumerable.Single(IEnumerable`1 source) +100
System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3(IEnumerable`1 sequence) +31
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +56
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +97
System.Linq.Queryable.Any(IQueryable`1 source) +233
Saxo.Publishing.Business.UserServiceOLD.IsUserInRole(User user, String roleName) in c:\Users\laho\Documents\GitHub\SaxoPublishing\Development\Saxo Publishing\Saxo.Publishing.Business\UserServiceOLD.cs:175
Saxo.Publishing.Web.Security.UserPrincipal.IsInRole(String role) in c:\Users\laho\Documents\GitHub\SaxoPublishing\Development\Saxo Publishing\Saxo.Publishing.Web\Security\User\UserPrincipal.cs:42
System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +146
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +333
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +421
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +143
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +978
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +53
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +538
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +83
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +531
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +447
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +127
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +388
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

非常感谢任何想法! :-)

尝试重写版本 1:

public bool IsUserInRole(User user, string roleName)
{
using (var repository = new SaxoPublishingEntities())
{

var users = repository.Users.Where(c => c.Id == user.Id);

if (users.Any())
{
var myUser = users.First();
var roles = repository.UserRoles.Where(userRole =>
userRole.Name == roleName && userRole.Users.Contains(myUser)).ToList();

if (roles.Any())
{
return true;
}
}
return false;
}
}

最佳答案

这是一条针对众所周知的 EF 限制的神秘消息:您不能在 Contains 中使用引用类型。而是这样做:

var userId = user.Id;
var roles = repository.UserRoles.Where(userRole => userRole.Name == roleName
&& userRole.Users.Select(u => u.Id).Contains(userId));

关于c# - Entity Framework - 索引(从零开始)必须大于或等于零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com