属性 使用 Entity Framework 5 和 Oracle 时,web api 控制器中为空

Property null in web api controller when using Entity Framework 5 and Oracle

当我尝试将 EF5 与 Oracle 一起使用时,我在新的 Web api 控制器中遇到问题。

我的控制器:

    public class DeviceV1Controller : ApiController
{
    private readonly IDevice _repository;

    public DeviceV1Controller()
    {
        IDevice _repository = new EFDeviceRepository();
    }

    [Route("api/Device/{hashImei}/app/{nome}")]
    public HttpResponseMessage Post(string hashImei, string nome, [FromBody] DeviceInfo deviceInfo)
    {
        _repository.Add(deviceInfo);
        return Request.CreateResponse(HttpStatusCode.OK);

    }
}

_repository 在构造函数中正确绑定,但是输入 Post api 这个变量变为空,我得到这个错误:

{
message: "An error has occurred."
exceptionMessage: "object reference not set to an instance of an object."
exceptionType: "System.NullReferenceException"
stackTrace: " in MpssApiRest.Controllers.DeviceV1Controller.Post(String hashImei, String nome, DeviceInfo deviceInfo) in c:\SVILUPPO\MpssApiRest\MpssApiRest\MpssApiRest\Controllers\DeviceV1Controller.cs:riga 28 in lambda_method(Closure , Object , Object[] ) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) in System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- Fine traccia dello stack da posizione precedente dove è stata generata l'eccezione --- in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
}

EFDeviceRepository具体class是:

    public class EFDeviceRepository : IDevice
{
    private readonly EntityDevice ctx;

    public EFDeviceRepository()
    {
        ctx = new EntityDevice();
    }

    public void Add(Models.V1.DeviceInfo deviceInfo)
    {
        EntityDevice ctx = new EntityDevice();
        MPSS_APP_DEVICE device = new MPSS_APP_DEVICE();
        device.HASHIMEI = deviceInfo.HashImei;
        ctx.MPSS_APP_DEVICE.Add(device);

        ctx.SaveChanges();
    }
}

谢谢


编辑:Web 请求示例(从评论中检索)

Ip Address: 192.168.1.129
Url: /myproject/api/device/123456/app/appname 
JSON: 
{ 
    "applicazione" : "Gestione Interventi", 
    "hashImei" : "123123121323123121", 
    "modello" : "Nexus 5", 
    "pushNotificatioToken" : "oifjwfijowfjfoiwjrgfoirwj42rohfoifrj",
     "sistemaOperativo" : "ANDROID", "versione" : "LOLLIPOP_MR1" 
} 

你的 Action 中 _repository 为 null 的原因是你没有在你的构造函数中初始化它。相反,您在构造函数中声明并初始化了一个同名的局部变量!

public class DeviceV1Controller : ApiController
{
    private readonly IDevice _repository;

    public DeviceV1Controller()
    {
        _repository = new EFDeviceRepository();
    }

    // ...
}