I wrote some code using C# and Entity Framework 6 (EF6) with MySql. The IDE is Visual Studio 2019 preview.
I installed these packages:
<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" />
</packages>
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))
{
connection.Open();
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
`Car_Update`
-- 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:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
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)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Car>().MapToStoredProcedures();
}
}
and
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')
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.