locked
How can I update a single column in a row? RRS feed

  • Question

  • Hi all;

    I've done a lot with ADO.NET but I'm totally new to Entity Frameworks. I have a question.

    I need to set a single column value in a given row. If I can set just the one column, then I don't need transactions (and that's a lot faster when there's a ton of these). But with Entity Frameworks it looks like I have to create a transaction, read the row, set the column value, then save the row.

    Is there a way to set just 1 column in a row?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Sunday, September 29, 2013 1:07 AM

Answers

  • Hi DavidThi808;

    Entity Framework needs to have the record to be updated in local memory, in the DbContext, therefore what you want to do is not possible. That said you can use the ExecuteStoreCommand to do what you want. This command bypasses the ObjectContext and just uses it connection to the database.

    You can use the ExecuteStoreCommand( CommandText, Parameters[] ) of the ObjectContext to execute a SQL native command on the server. If you are using Entity Framework with an ObjectContext already then the first two steps will need to be modified first step is to create the ObjectContext and NOT dbContext and the second step is NOT done at all.

    // Sample Code

    // If you are using a DbContect DbContext dbContext = New DbContext(); // Get a reference to the underlaying ObjectContext like so ObjectContext context = ((IObjectContextAdapter)dbContext).ObjectContext; // Create a SQL Update command To perform the update on the table // this command example is used to update the AdventureWorks db Product table // ListPrice column. string sqlCmd = "USE AdventureWorks2012;" + "UPDATE Production.Product" + "SET ListPrice = ListPrice * 2;"; // Then execute the command to update the database on the server. The // parameters is a array of Objects of values to be updated of null if no // parameters. context.ExecuteStoreCommand(sqlCmd, parameters);


      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Fred Bao Monday, September 30, 2013 4:30 AM
    • Marked as answer by DavidThielen Monday, September 30, 2013 2:32 PM
    Sunday, September 29, 2013 2:00 AM

All replies

  • Hi DavidThi808;

    Entity Framework needs to have the record to be updated in local memory, in the DbContext, therefore what you want to do is not possible. That said you can use the ExecuteStoreCommand to do what you want. This command bypasses the ObjectContext and just uses it connection to the database.

    You can use the ExecuteStoreCommand( CommandText, Parameters[] ) of the ObjectContext to execute a SQL native command on the server. If you are using Entity Framework with an ObjectContext already then the first two steps will need to be modified first step is to create the ObjectContext and NOT dbContext and the second step is NOT done at all.

    // Sample Code

    // If you are using a DbContect DbContext dbContext = New DbContext(); // Get a reference to the underlaying ObjectContext like so ObjectContext context = ((IObjectContextAdapter)dbContext).ObjectContext; // Create a SQL Update command To perform the update on the table // this command example is used to update the AdventureWorks db Product table // ListPrice column. string sqlCmd = "USE AdventureWorks2012;" + "UPDATE Production.Product" + "SET ListPrice = ListPrice * 2;"; // Then execute the command to update the database on the server. The // parameters is a array of Objects of values to be updated of null if no // parameters. context.ExecuteStoreCommand(sqlCmd, parameters);


      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Fred Bao Monday, September 30, 2013 4:30 AM
    • Marked as answer by DavidThielen Monday, September 30, 2013 2:32 PM
    Sunday, September 29, 2013 2:00 AM
  • that'll work great - thanks - dave

    Who will win The Windward International Collegiate Programming Championships?

    Monday, September 30, 2013 2:33 PM
  •   

    Not a problem Dave, glad to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, September 30, 2013 2:35 PM
  • This is very helpful toward what I want to do... I just posed a question that does not meet your requirement for simplicity so I would be happy with an answer to this: If I apply this routine above and update my server, will it somehow carry over to my gridview?  Asked otherwise, I have a gridview in which I have calculated a single column. I want to update the particular selected cell and cannot figure it out

    eps123

    • Proposed as answer by eps123 Tuesday, December 1, 2015 6:03 PM
    Tuesday, December 1, 2015 6:03 PM