0

In my angular application, I call the API by passing an object. I just have one button click event. The business logic contains multiple database operation by this simple click.

On the server side, I have to decide to insert or update or delete records from entity framework.

My question is for the convenience, can I mix different type together in one method?

Code sample:

[HttpPatch]
[HttpPost]
[HttpDelete]
public ActionResult InsertOrUpdateOrDeleteByCondition([Required][FromBody]MyDto body)
{
    if(body.value == "condition1")
    {
       dbContext.MyEntity.Add(body);
    }
    else if(body.value == "condition2")
    {
       dbContext.MyEntity.Update(body);
    }
    else if(body.value == "condition3")
    {
        // delete first
        // then insert new value
    }
    else
    {
       dbContext.MyEntity.Remove(body);
    }
    dbContext.SaveChanges();
    return Json("Good job");
}

I set a break point at the line SaveChanges(). It did reach there, however I found the table is not changed.

Hello
  • 796
  • 8
  • 30

1 Answers1

0

You can combine else if's using Boolean logic like && || ! even if they're different types

tommmm
  • 182
  • 1
  • 4