gpt4 book ai didi

c# - API 中的 post 方法未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:44 26 4
gpt4 key购买 nike

我有一个简单的 api 方法将数据插入数据库,postman man 中的方法显示成功,但数据没有插入数据库,这是我的代码

private Utilities uti = new Utilities();
private readonly ApplicationDBContext db;
public AppraisalController(ApplicationDBContext context)
{
db = context;
}
//INSERT API FOR AppraisalIdentity table
[AllowAnonymous]
[Route("api/appraiseinsert")]
[HttpPost]
public IActionResult Create([FromBody] AppraisalIdentity cre)
{
if (cre == null)
{
return BadRequest();
}
using (var transaction = db.Database.BeginTransaction())
{
try
{
#region Appraisal Insert
var apprais = new AppraisalIdentity
{
AppraisalName = cre.AppraisalName,
IsCurrent = cre.IsCurrent,
CompanyID = cre.CompanyID,
DateAdded = cre.DateAdded
};
db.AppraisalIdentity.Add(apprais);
db.SaveChanges();
#endregion
}
catch (Exception ex)
{
transaction.Rollback();
return Json(new
{
statusCode = ex.Message
});
}

}

return Json(new
{
statusCode = "Success"
});
}

我不知道,也许我的代码中某处存在我不知道的错误,但事实是,在 postman 中,api 返回成功,但它没有在 db 中插入任何内容。谢谢

最佳答案

由于您正在使用事务,我想您需要在调用 db.SaveChanges() 之后提交事务,如下所示:

db.SaveChanges();
transaction.Commit();

关于c# - API 中的 post 方法未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com