SaveChanges is called inside a loop, so each round trip saves one change

tx0/csharp:save-changes-in-looprecommendedwarningperformance/iodepth fastperformance

What it finds

Each call of SaveChanges or SaveChangesAsync opens a transaction and a round trip to the database. Inside a loop over many items that is one transaction per item; adding all of them and saving once after the loop is usually much faster and atomic.

From the rule's source, save_changes_in_loop.rs.

Example from the rule's tests
using System.Collections.Generic; class Import{    void Run(Db db, List<int> ids)    {        foreach (var id in ids)        {            db.Add(id);            db.SaveChanges();tx0 reports: SaveChanges runs once per loop round        }        db.SaveChanges();    }} class Db{    public void Add(int id) { }     public int SaveChanges() => 0;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:save-changes-in-loop" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:save-changes-in-loop] reason="..."

tx0 explain tx0/csharp:save-changes-in-loop shows the same in the terminal. Found a false positive? Report it.