Why does context.SaveChanges() update every row in the table?

.net c# entity-framework-6 mysql visual-studio


I wrote some code using C# and Entity Framework 6 (EF6) with MySql. The IDE is Visual Studio 2019 preview.

I installed these packages:

  <package id="EntityFramework" version="6.2.0" targetFramework="net461" />
  <package id="MySql.Data" version="6.10.8" targetFramework="net461" />
  <package id="MySql.Data.Entity" version="6.10.8" targetFramework="net461" />

Inserting and Selecting are works correctly but Updating works wired.

I put some number(something like 2 or 100) into tbx_CarId and I expected that EF6 updates only ONE row, but it updates every low in the table.

private void btn_update_Click(object sender, EventArgs e)
    using (MySqlConnection connection = new MySqlConnection(connectionString))

        using (Parking context = new Parking(connection, false))
            context.Database.Log = (string message) => { Console.WriteLine(message); };

            int targetId = Int32.Parse(tbx_CarId.Text);
            var blogs = from b in context.Cars
                        where b.CarId == targetId
                        select b;
            Car item = blogs.Single();
            item.Model = tbx_NewModel.Text;
            int numOfSavedLows = context.SaveChanges();
            Console.WriteLine("numOfSavedLows: " + numOfSavedLows.ToString());

The method context.SaveChanges() always returns an exact number, 1, and on the console window, "numOfSavedLows: 1" is printed.

But the every row is changed whenever I execute that method.

Also, the logger of EF6 writes on the console something like this:

Started transaction at 2019-01-03 오후 6:47:59 +09:00
-- CarId: '2' (Type = Int32, IsNullable = false)
-- Model: 'new value of the model' (Type = String, IsNullable = false, Size = 5)
-- Year: '2013' (Type = Int32, IsNullable = false)
-- Manufacturer: 'Dodge' (Type = String, IsNullable = false, Size = 5)
-- Executing at 2019-01-03 오후 6:47:59 +09:00
-- Completed in 6 ms with result: 16

Committed transaction at 2019-01-03 오후 6:47:59 +09:00
Disposed transaction at 2019-01-03 오후 6:47:59 +09:00

Please look at the line -- Completed in 6 ms with result: 16 The number(in this case, 16) is the count of every row in Car table.

Why does EF6 update every low? How can I fix it?

The rest of the codes are here:

class Parking : DbContext
    public DbSet<Car> Cars { get; set; }

    public Parking()
      : base()
        // constructor is empty

    // Constructor to use on a DbConnection that is already opened
    public Parking(DbConnection existingConnection, bool contextOwnsConnection)
      : base(existingConnection, contextOwnsConnection)
        // constructor is empty

    protected override void OnModelCreating(DbModelBuilder modelBuilder)


class Car
    public int CarId { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public string Manufacturer { get; set; }

Most of the codes are from here: https://dev.mysql.com/doc/connectors/en/connector-net-entityframework60.html

update 1: The table Cars looks like this (The schema and table are generated by EF6 automatically)

update 2: The values of the rows are like this: Before and After (args are 8, 'new value')

1/3/2019 11:09:48 AM

Popular Answer

As suggested in official EF6 GitHub,

Removing the MapToStoredProcedure in my DbContext fixes the issue.

I would tell you to try removing the "MapToStoredProcedures()" code and see if you get any different result.

1/3/2019 9:40:29 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow