我在Visual Studio MVC Web API中创建了一个Post action,并在Parameter中传递了tbl_employee,当我通过Postman检查action时,它会将空值插入数据库中的表中。
在Visual Studio 2019上工作,框架版本为4.5。
public HttpResponseMessage Post([FromBody]tbl_employee stbl_Employee)
{
try
{
se.tbl_employee.Add(stbl_Employee);
se.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created,stbl_Employee);
message.Headers.Location = new Uri(Request.Requesturi +
stbl_Employee.emp_id.ToString());
return message;
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest,ex);
}
}
public partial class tbl_employee
{
public int emp_id { get; set; }
public string emp_name { get; set; }
public Nullable<int> emp_salary { get; set; }
public Nullable<int> emp_department { get; set; }
public virtual tbl_department tbl_department { get; set; }
}
输出为数据库中所有值均为空,而邮递员给出错误。