c# - EF4 update a value for all rows in a table without doing a select -


i need reset boolean field in specific table before run update. table have 1 million or records , i'd prefer not have have select before update taking time.

basically need in code produce following in tsql

update tablename  set flag = false  flag = true 

i have thing close need here http://www.aneyfamily.com/terryandann/post/2008/04/batch-updates-and-deletes-with-linq-to-sql.aspx have yet implement wondering if there more standard way.

to keep within restrictions have project, cant use sprocs or directly write tsql in executestorecommand parameter on context believe can do.

i'm aware need may not directly supported in ef4 , may need @ sproc job [in total absence of other way] need explore possibilities first. in ef ideal world call above update flag possible or alternatively possible entity id , boolean flag minus associated entities , loop through entity , set flag , single savechanges call, may not way works.

any ideas,

thanks in advance. liam

i go stakeholder introduced restirctions not using sql or sproc directly , present him these facts:

  • updates in orm (like entity framework) work way: load object perform modification save object. valid way.
  • obviously in case mean load 1m entities , execute 1m updates separately (ef has no command batching - each command runs in own roundtrip db) - absolutely useless solution.
  • the example provided looks interesting linq-to-sql. not entity framework. unless implement can't sure work ef, because infrastructure in ef more complex. can spent several man days doing without result - should approved stakeholder.
  • solution sproc or direct sql take few minutes , work.
  • in both solution have deal problem. if have materialized entities , run such command (via mentioned extension or via sql) these changes not mirrored in loaded entities - have iterate them , set flag.
  • both scenarios break unit of work because data changes executed before unit of work completed.

it using right tool right requirement.

btw. loading of realted tables can avoided. query run. not use include , not access navigation properties (in case of lazy loading) , not load relation.

it possible select id (via projection), create dummy entity (set id , and flag true) , execute updates of flag still execute 1m updates.

using(var mycontext = new mycontext(connectionstring)) {   var query = o in mycontext.myentities               o.flag == false               select o.id;   foreach (var id in query)   {     var entity = new myentity       {         id = id,         flag = true       };     mycontext.attach(entity);     mycontext.objectstatemanager.getobjectstateentry(entity).setmodifiedproperty("flag");   }    mycontext.savechanges(); } 

moreover work in empty object context (or @ least no entity updated table can attached context). in scenarios running before other updates require 2 objectcontext instances = manually sharing dbconnection or 2 database connections , in case of transactions = distributed transaction , performance hit.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -