5

我有一个项目,其中有我的 API 类,并且在该项目中我生成了 Database.cs。在我的 MVC 项目中,我引用了 API。运行应用程序时,我收到如下错误消息,任何人都可以帮忙,因为我不明白为什么我会收到此错误消息。

http://www.garethelms.org/2011/05/help-getting-started-with-petapoco/#comment-69,我可以看到自从安装 petaPOCo 以来,所有 GetSetMethod() 都是 GetSetMethod(true) .

值不能为空。参数名称:meth 描述:当前web请求执行过程中发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentNullException:值不能为空。参数名称:meth

源错误:

第 1990 行:} 第 1991 行:第 1992 行:il.Emit(OpCodes.Callvirt, pc.PropertyInfo.GetSetMethod(true)); // poco Line 1993: Handled = true; 第 1994 行:}

源文件:C:\Dev\MyProjectTest\Code\API\Models\PetaPoco.cs 行:1992

堆栈跟踪:

[ArgumentNullException:值不能为空。参数名称:meth]
System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, MethodInfo meth) +9492330 PetaPoco.PocoData.GetFactory(String sql, String connString, Boolean ForceDateTimesToUtc, Int32 firstColumn, Int32 countColumns, IDataReader r) in C: \Dev\MyProjectTest\Code\API\Models\PetaPoco.cs:1992
PetaPoco.d__7 1..ctor(IEnumerable 1 source) +58 PetaPoco.Database.Fetch(String sql, Object[] args) 在 C:\Dev\ MyProjectTest\Code\API\Models\PetaPoco.cs:601 PecaTest.NewProject.API.Customer.LoadSortedByName() 在 C:\Dev\MyProjectTest\Code\API\Customer.cs:681.MoveNext() in C:\Dev\MyProjectTest\Code\API\Models\PetaPoco.cs:765
System.Collections.Generic.List
1 collection) +327 System.Linq.Enumerable.ToList(IEnumerable


PecaTest.NewProject.MvcUI.Models.CustomerListModel..ctor() 在 C:\Dev\MyProjectTest\Code\MvcUI\Models\CustomerListModel.cs:14
PecaTest.NewProject.MvcUI.Controllers.CustomerController.GetIndexView() 在 C:\ Dev\MyProjectTest\Code\MvcUI\Controllers\CustomerController.cs:82
PecaTest.NewProject.MvcUI.Controllers.CustomerController.Index() 在 C:\Dev\MyProjectTest\Code\MvcUI\Controllers\CustomerController.cs:19
lambda_method(Closure , ControllerBase , Object[] ) +96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器, Object[] 参数) +51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 参数) +52 System. Web.Mvc.<> c_DisplayClassd.b2 parameters) +409
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary

_a() +127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func 1 继续) +436 System.Web.Mvc.<>c_ DisplayClassf.b _c() +61 System.Web.Mvc. ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 2 参数) +305 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830 System.Web.Mvc.Controller.ExecuteCore() +136 System.Web.Mvc. ControllerBase.Execute(RequestContext requestContext) +111 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 System.Web.Mvc.<>c_1 continuation) +436
System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func

1 filters, ActionDescriptor actionDescriptor, IDictionary



DisplayClass8.b _4() +65
System.Web.Mvc.Async.<>c_ DisplayClass1.b _0() +44
System.Web.Mvc.Async.<>c__DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +42 System.Web.Mvc.Async.WrappedAsyncResult1.End() +140 System.Web.Mvc .Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, 布尔值和同步完成)+184

-------------------------------------------------- ------------------ 版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.272


客户类

` private int id = 0;
    public  int ID
    {
        get { return id; }
        set { id = ID; }
    }

    protected string name;
    public Customer() { }

    public Customer(string name)
    {
        this.name = name;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public override string ToString()
    {
        return name;
    }

`

4

4 回答 4

11

使接受的答案更加精确。当我的 POCO 中有一个与数据库表中列的名称匹配的属性但我没有该属性的设置器时,我遇到了这个问题。就我而言,这是我自己计算的属性,所以我并不真正需要表中的数据,我打算将其设为只读。添加一个空的二传手为我解决了这个问题。

一个可能更好的选择是将 Ignore 属性添加到该属性。

我最终最终从表中删除了该列。

于 2017-01-12T04:54:44.153 回答
4

您的 poco 是否有无参数构造函数,即;没有论据?它需要一个。如果您使用参数创建了自己的构造函数,则不会有默认的无参数构造函数,这可能会导致反射代码失败。也许就是这样。

于 2012-02-04T09:24:01.253 回答
2

问题确实出在 POCO 上,因为我错过了设置和获取 ID

于 2012-02-04T11:31:30.000 回答
1

我在 dapper 中遇到了这个问题,在我的 poco 对象中,我在解决了删除私有问题后将一个字段定义为私有 geter

    public bool isObsolete { **private** get; set; }

改成

    public bool isObsolete { get; set; }
于 2020-10-24T16:31:44.910 回答