Let's say I have a method that loads some entities from database, makes an API call for each, gets a token from a third party and saves them.
The API call is critical so we want each call to be logged into db instantly.
So I put context.SaveChanges in CriticalAPI, to save each log separately.
The problem is that this SaveChanges also saves Posts entities modified in Method().
I want CriticalAPI to only save logs, not other objects. One way is to create another context and use in that method, but it violates dependency injection because I should instantiate a new context in my method.
What is the correct way to achieve this requirements?
public void Method(){
var entities = context.Posts.Where(/* something */).ToList();
foreach (var entity in entities){
var result = CriticalAPI(entity.Id);
entity.Token = result;
}
context.SaveChanges();
}
public int CriticalAPI(int id){
var token = /* do something critical */
context.Logs.Add(new Log(){
entityId = id
});
context.SaveChanges();
return token;
}