C
C#4w ago
TJacken

Is having two DbContext on same base antipattern or bad approach?

In company (so there is no much freedom to do with other tools I need to solve this using EF Core), we are using CQRS with MediatR, and every command handler is wrapped in a transaction. So if something fails, we roll back the transaction. Here’s the issue: I need to save certain data even if an exception occurs. The main data should be rolled back, but part of the data needs to be saved in the database. I know I can use two separate DB contexts to solve this issue, but is this a good approach? Is there a better way to partially commit data to the database?
2 Replies
jcotton42
jcotton424w ago
@TJacken save points may be an option if supported by your database https://learn.microsoft.com/en-us/ef/core/saving/transactions#savepoints
Transactions - EF Core
Managing transactions for atomicity when saving data with Entity Framework Core
TJacken
TJackenOP4w ago
@jcotton42 thx, didn't know for this feature on ef core. Is there any side effects with using save point? Not sure if db support this, need to check. Do you have idea for any other approach using ef core?
Want results from more Discord servers?
Add your server