Update a record without first querying?

前端 未结 7 581
南方客
南方客 2020-11-28 04:29

Lets say I query the database and load a list of items. Then I open one of the items in a detail view form, and instead of re-querying the item out of the database, I create

相关标签:
7条回答
  • 2020-11-28 05:19

    You can also use direct SQL against the database using the context of the datastore. Example:

    dataEntity.ExecuteStoreCommand
       ("UPDATE items SET itemstatus = 'some status' WHERE id = 123 ");
    

    For performance reasons, you may want to pass in variables instead of a single hard coded SQL string. This will allow SQL Server to cache the query and reuse with parameters. Example:

    dataEntity.ExecuteStoreCommand
       ("UPDATE items SET itemstatus = 'some status' WHERE id = {0}", new object[] { 123 });
    

    UPDATE - for EF 6.0

    dataEntity.Database.ExecuteSqlCommand
           ("UPDATE items SET itemstatus = 'some status' WHERE id = {0}", new object[] { 123 });
    
    0 讨论(0)
提交回复
热议问题