首页 > 解决方案 > Using a Web API to perform 2 DB operations

问题描述

I am doing my first tries with Web API and I have the following situation.
I need to perform 2 operations in a DB which must run inside a transaction:

1) Insert a record in one table (POST).
2) Update a record in another table (PUT).

Is it possible to do both in one call to a Web API? If so, what verb should I specify: POST, PUT or another one?

With respect,
Jorge Maldonado

标签: asp.net-web-api2

解决方案


首先,您的 Web API 根本不应该直接调用您的数据库。Web API 应该调用一个服务层,然后调用您的数据库或调用 repos。

服务层可以根据需要对数据库进行尽可能多的调用,以完成任何业务规则。

HTTP 动词通常不是一个动作的硬性规则,但一般来说,POST 用于创建,PUT 用于更新。在这种情况下,如果您正在创建一条记录并更新另一条记录,我会默认使用 POST。


推荐阅读