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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















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')










share|improve this question

























  • Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

    – Backs
    Jan 3 at 10:04






  • 3





    That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

    – Gert Arnold
    Jan 3 at 10:10











  • Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

    – Harald Coppoolse
    Jan 3 at 11:12











  • When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

    – mjwills
    Jan 3 at 11:22













  • Can you go into database and find Car_Update procedure?

    – Umair Anwaar
    Jan 3 at 13:02


















2















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')










share|improve this question

























  • Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

    – Backs
    Jan 3 at 10:04






  • 3





    That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

    – Gert Arnold
    Jan 3 at 10:10











  • Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

    – Harald Coppoolse
    Jan 3 at 11:12











  • When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

    – mjwills
    Jan 3 at 11:22













  • Can you go into database and find Car_Update procedure?

    – Umair Anwaar
    Jan 3 at 13:02














2












2








2


1






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')










share|improve this question
















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')







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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:09









marc_s

585k13011251272




585k13011251272










asked Jan 3 at 10:00









Mark Kim 김마크Mark Kim 김마크

142




142













  • Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

    – Backs
    Jan 3 at 10:04






  • 3





    That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

    – Gert Arnold
    Jan 3 at 10:10











  • Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

    – Harald Coppoolse
    Jan 3 at 11:12











  • When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

    – mjwills
    Jan 3 at 11:22













  • Can you go into database and find Car_Update procedure?

    – Umair Anwaar
    Jan 3 at 13:02



















  • Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

    – Backs
    Jan 3 at 10:04






  • 3





    That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

    – Gert Arnold
    Jan 3 at 10:10











  • Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

    – Harald Coppoolse
    Jan 3 at 11:12











  • When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

    – mjwills
    Jan 3 at 11:22













  • Can you go into database and find Car_Update procedure?

    – Umair Anwaar
    Jan 3 at 13:02

















Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

– Backs
Jan 3 at 10:04





Are you sure, that 16 rows were updated? Maybe table contains index, and it was rebuild? Have you used profiler to see generated sql code?

– Backs
Jan 3 at 10:04




3




3





That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

– Gert Arnold
Jan 3 at 10:10





That can only happen when CarId isn't unique and there are 16 records having CarId == 2.

– Gert Arnold
Jan 3 at 10:10













Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

– Harald Coppoolse
Jan 3 at 11:12





Did you check the State of the Entities in the ChangeTracker of your DbContext? All of them should equal EntityState.Unchanged, except the entity you changed.

– Harald Coppoolse
Jan 3 at 11:12













When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

– mjwills
Jan 3 at 11:22







When you run a database trace (or use hibernatingrhinos.com/products/efprof) to see the exact commands being sent to the database, what is being submitted?

– mjwills
Jan 3 at 11:22















Can you go into database and find Car_Update procedure?

– Umair Anwaar
Jan 3 at 13:02





Can you go into database and find Car_Update procedure?

– Umair Anwaar
Jan 3 at 13:02












3 Answers
3






active

oldest

votes


















0














Context.Savechanges() finds all changes in all dbsets(entities) and commit all of them in the database, it is a draw in Entity framework.






share|improve this answer
























  • Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

    – Harald Coppoolse
    Jan 3 at 11:10



















0














I guess your entity doesn't have a primary column defined.
That way, Entity Framework cannot select the correlating data in the table to your entity and updates every row where "everything" (every entry matches the primary key definition) meets the condition.
You need to add something like



[Key, Column("Car_ID_IN_DB"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CarId{ get; set;}


to your modeled Cars table on your CarId (you don't show this definition)



This only works if you are using CodeFirst, if you are using DbFirst you should check the generated entity of Carsif it's modeled correctly and if you modeled a primary key in your DB.






share|improve this answer


























  • But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

    – Mark Kim 김마크
    Jan 3 at 10:49





















0














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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019980%2fwhy-does-context-savechanges-update-every-row-in-the-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Context.Savechanges() finds all changes in all dbsets(entities) and commit all of them in the database, it is a draw in Entity framework.






    share|improve this answer
























    • Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

      – Harald Coppoolse
      Jan 3 at 11:10
















    0














    Context.Savechanges() finds all changes in all dbsets(entities) and commit all of them in the database, it is a draw in Entity framework.






    share|improve this answer
























    • Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

      – Harald Coppoolse
      Jan 3 at 11:10














    0












    0








    0







    Context.Savechanges() finds all changes in all dbsets(entities) and commit all of them in the database, it is a draw in Entity framework.






    share|improve this answer













    Context.Savechanges() finds all changes in all dbsets(entities) and commit all of them in the database, it is a draw in Entity framework.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 10:16









    isaeidisaeid

    232216




    232216













    • Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

      – Harald Coppoolse
      Jan 3 at 11:10



















    • Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

      – Harald Coppoolse
      Jan 3 at 11:10

















    Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

    – Harald Coppoolse
    Jan 3 at 11:10





    Still, he only changed one row. The ChangeTracker of the DbContext should contain all elements, but most of them should have a State equal to EntityState.Unchanged.

    – Harald Coppoolse
    Jan 3 at 11:10













    0














    I guess your entity doesn't have a primary column defined.
    That way, Entity Framework cannot select the correlating data in the table to your entity and updates every row where "everything" (every entry matches the primary key definition) meets the condition.
    You need to add something like



    [Key, Column("Car_ID_IN_DB"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CarId{ get; set;}


    to your modeled Cars table on your CarId (you don't show this definition)



    This only works if you are using CodeFirst, if you are using DbFirst you should check the generated entity of Carsif it's modeled correctly and if you modeled a primary key in your DB.






    share|improve this answer


























    • But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

      – Mark Kim 김마크
      Jan 3 at 10:49


















    0














    I guess your entity doesn't have a primary column defined.
    That way, Entity Framework cannot select the correlating data in the table to your entity and updates every row where "everything" (every entry matches the primary key definition) meets the condition.
    You need to add something like



    [Key, Column("Car_ID_IN_DB"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CarId{ get; set;}


    to your modeled Cars table on your CarId (you don't show this definition)



    This only works if you are using CodeFirst, if you are using DbFirst you should check the generated entity of Carsif it's modeled correctly and if you modeled a primary key in your DB.






    share|improve this answer


























    • But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

      – Mark Kim 김마크
      Jan 3 at 10:49
















    0












    0








    0







    I guess your entity doesn't have a primary column defined.
    That way, Entity Framework cannot select the correlating data in the table to your entity and updates every row where "everything" (every entry matches the primary key definition) meets the condition.
    You need to add something like



    [Key, Column("Car_ID_IN_DB"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CarId{ get; set;}


    to your modeled Cars table on your CarId (you don't show this definition)



    This only works if you are using CodeFirst, if you are using DbFirst you should check the generated entity of Carsif it's modeled correctly and if you modeled a primary key in your DB.






    share|improve this answer















    I guess your entity doesn't have a primary column defined.
    That way, Entity Framework cannot select the correlating data in the table to your entity and updates every row where "everything" (every entry matches the primary key definition) meets the condition.
    You need to add something like



    [Key, Column("Car_ID_IN_DB"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CarId{ get; set;}


    to your modeled Cars table on your CarId (you don't show this definition)



    This only works if you are using CodeFirst, if you are using DbFirst you should check the generated entity of Carsif it's modeled correctly and if you modeled a primary key in your DB.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 10:41

























    answered Jan 3 at 10:29









    ChrᴉzChrᴉz

    1,025319




    1,025319













    • But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

      – Mark Kim 김마크
      Jan 3 at 10:49





















    • But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

      – Mark Kim 김마크
      Jan 3 at 10:49



















    But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

    – Mark Kim 김마크
    Jan 3 at 10:49







    But the table Cars has a primary key... CarId column is the primary key. check this out. i.stack.imgur.com/x1cle.png

    – Mark Kim 김마크
    Jan 3 at 10:49













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 21:40









        Fabio M.Fabio M.

        1045




        1045






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019980%2fwhy-does-context-savechanges-update-every-row-in-the-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory