【问题标题】:Post {FromBody] from data model to execute a procedure从数据模型发布 {FromBody] 以执行过程
【发布时间】:2018-05-02 14:55:37
【问题描述】:

我认为我在这里的方向是正确的,但不能完全解决这个问题。 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, 字符串,字符串,字符串,字符串,字符串,字符串,布尔值? bool?)' LloydsVirtualAssistant C:\Users\Noel.Purdie2\OneDrive - Mitie\MITIE 解决方案交付 Team\ChatAI\LloydsVirtualAssistant\LloydsVirtualAssistant\Controllers\CreateController.cs 28 活跃

我知道使用 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);
        }
    }
}

【问题讨论】:

  • 可以分享usp_CreateCase方法的实现吗?
  • 好的,谢谢!我现在就去启动我的笔记本电脑

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


【解决方案1】:

您以错误的方式将数据传递给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));
}

【讨论】:

  • 谢谢。祝福你。我担心得睡不着...我会在凌晨 4 点起床尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 2020-06-17
相关资源
最近更新 更多