从数据模型发布{FromBody]以执行过程

时间:2018-04-15 19:47:08

标签: asp.net-web-api ado.net

我认为我在这里走在正确的轨道上,但不能完全理解这一点。对于WebAPI和c#我在SQL中的背景非常新,我真的可以使用提示。非常感谢您的任何反馈。

using VirtualAssistant.Models;
using VirtualAssistant.DataModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.Http.Description;
using System.Collections;
using System.Data.Entity.Core.Objects;


namespace VirtualAssistant.Controllers
{
    public class CreateController : ApiController
    {

        // POST: ChatAi/CreateCase
        [Route("CreateCase")]

        public IHttpActionResult Post([FromBody]usp_CreateCase_Submit createCase_Submit )
        {
            usp_CreateCase_Result results = new CreateCaseEntities().usp_CreateCase(createCase_Submit).FirstOrDefault(); 

            if (results.caseId == null)
            {
                return NotFound();
            }
            return Ok(results);
        }
    }
}

我正在使用这些数据模型来帮助支持Post [FromBody]

namespace VirtualAssistant.DataModel
{
    using System;

    public partial class usp_CreateCase_Result
    {
        public string referenceId { get; set; }
        public Nullable<int> caseId { get; set; }
        public string slaPriorityName { get; set; }
        public string helpDeskName { get; set; }
        public string helpDeskPhoneNumber { get; set; }
        public string providerCompanyName { get; set; }
        public string providerDivisionName { get; set; }
        public string serviceDetailName { get; set; }
        public string serviceTypeName { get; set; }
        public string serviceStreamName { get; set; }
        public Nullable<System.DateTime> slaAllocateBy { get; set; }
        public Nullable<System.DateTime> slaAttendBy { get; set; }
        public Nullable<System.DateTime> slaCompleteBy { get; set; }
        public string caseStage { get; set; }
        public string statusCode { get; set; }
        public string statusMessage { get; set; }
    }

    public partial class usp_CreateCase_Submit
    {
        public Nullable<int> personId { get; set; }
        public Nullable<int> locationId { get; set; }
        public Nullable<int> serviceDetailId { get; set; }
        public Nullable<int> linkedCaseId { get; set; }
        public string contactFullName { get; set; }
        public string contactPhoneNumber { get; set; }
        public string contactMobileNumber { get; set; }
        public string contactEmailAddres { get; set; }
        public string problemNote { get; set; }
        public string costCode { get; set; }
        public string authoriserEmailAddress { get; set; }
        public bool isDraft { get; set; }
        public bool isOnlineEmergencyConfirmed { get; set; }
    }

}

我要处理的错误是:

  

严重级代码描述项目文件行抑制状态   错误CS7036没有给出对应的参数   需要正式参数'locationId'   'CreateCaseEntities.usp_CreateCase(int?,int?,int?,int?,string,   string,string,string,string,string,string,bool?,   bool?)'LloydsVirtualAssistant C:\ Users \ Noel.Purdie2 \ OneDrive -   Mitie \ MITIE解决方案交付   Team \ ChatAI \ LloydsVirtualAssistant \ LloydsVirtualAssistant \ Controllers \ CreateController.cs 28 Active

我明白使用json我只能在from中有一个输入。我工作的团队坚持[FromBody]没有使用URI。我可以发现的每个例子都不包括使用数据库对象。

我正在使用数据模型导入函数

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace VirtualAssistant.DataModel
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;

    public partial class CreateCaseEntities : DbContext
    {
        public CreateCaseEntities()
            : base("name=CreateCaseEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }


        public virtual ObjectResult<usp_CreateCase_Result> usp_CreateCase(Nullable<int> personId, Nullable<int> locationId, Nullable<int> serviceDetailId, Nullable<int> linkedCaseId, string contactFullName, string contactPhoneNumber, string contactMobileNumber, string contactEmailAddress, string problemNote, string costCode, string authoriserEmailAddress, Nullable<bool> isDraft, Nullable<bool> isOnlineEmergencyConfirmed)
        {
            var personIdParameter = personId.HasValue ?
                new ObjectParameter("PersonId", personId) :
                new ObjectParameter("PersonId", typeof(int));

            var locationIdParameter = locationId.HasValue ?
                new ObjectParameter("LocationId", locationId) :
                new ObjectParameter("LocationId", typeof(int));

            var serviceDetailIdParameter = serviceDetailId.HasValue ?
                new ObjectParameter("ServiceDetailId", serviceDetailId) :
                new ObjectParameter("ServiceDetailId", typeof(int));

            var linkedCaseIdParameter = linkedCaseId.HasValue ?
                new ObjectParameter("LinkedCaseId", linkedCaseId) :
                new ObjectParameter("LinkedCaseId", typeof(int));

            var contactFullNameParameter = contactFullName != null ?
                new ObjectParameter("ContactFullName", contactFullName) :
                new ObjectParameter("ContactFullName", typeof(string));

            var contactPhoneNumberParameter = contactPhoneNumber != null ?
                new ObjectParameter("ContactPhoneNumber", contactPhoneNumber) :
                new ObjectParameter("ContactPhoneNumber", typeof(string));

            var contactMobileNumberParameter = contactMobileNumber != null ?
                new ObjectParameter("ContactMobileNumber", contactMobileNumber) :
                new ObjectParameter("ContactMobileNumber", typeof(string));

            var contactEmailAddressParameter = contactEmailAddress != null ?
                new ObjectParameter("ContactEmailAddress", contactEmailAddress) :
                new ObjectParameter("ContactEmailAddress", typeof(string));

            var problemNoteParameter = problemNote != null ?
                new ObjectParameter("ProblemNote", problemNote) :
                new ObjectParameter("ProblemNote", typeof(string));

            var costCodeParameter = costCode != null ?
                new ObjectParameter("CostCode", costCode) :
                new ObjectParameter("CostCode", typeof(string));

            var authoriserEmailAddressParameter = authoriserEmailAddress != null ?
                new ObjectParameter("AuthoriserEmailAddress", authoriserEmailAddress) :
                new ObjectParameter("AuthoriserEmailAddress", typeof(string));

            var isDraftParameter = isDraft.HasValue ?
                new ObjectParameter("IsDraft", isDraft) :
                new ObjectParameter("IsDraft", typeof(bool));

            var isOnlineEmergencyConfirmedParameter = isOnlineEmergencyConfirmed.HasValue ?
                new ObjectParameter("IsOnlineEmergencyConfirmed", isOnlineEmergencyConfirmed) :
                new ObjectParameter("IsOnlineEmergencyConfirmed", typeof(bool));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_CreateCase_Result>("usp_CreateCase", personIdParameter, locationIdParameter, serviceDetailIdParameter, linkedCaseIdParameter, contactFullNameParameter, contactPhoneNumberParameter, contactMobileNumberParameter, contactEmailAddressParameter, problemNoteParameter, costCodeParameter, authoriserEmailAddressParameter, isDraftParameter, isOnlineEmergencyConfirmedParameter);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在以错误的方式将数据传递给usp_CreateCase方法,您需要更新usp_CreateCase参数以获取usp_CreateCase_Submit类型的一个对象

usp_CreateCase(usp_CreateCase_Submit createCase_Submit)
{
    var personIdParameter = createCase_Submit.personId.HasValue ?
                new ObjectParameter("PersonId", createCase_Submit.personId) :
                new ObjectParameter("PersonId", typeof(int));
}