Friday 15 July 2011

entity framework - Change tracking behavior changed between EF6 and EF4. How to revert back to Ef4 behavior? -



entity framework - Change tracking behavior changed between EF6 and EF4. How to revert back to Ef4 behavior? -

in ef4 loading entity db , setting property same value had still cause update issued db.

in ef6, entity seems aware property value has not changed , not cause update issued db.

is possible ef4 behavior in ef6?

what have tried:

set "context.configuration.autodetectchangesenabled = false;". did not work. hail mary: context.configuration.validateonsaveenabled = false; did not work. ((iobjectcontextadapter) context).objectcontext.contextoptions.uselegacypreservechangesbehavior = true; did not work

the reason need old ef4 behavior because code used depend on triggers update fields (modifieddate) , ef4 cause save , ef6 doesnt , cause major regression head-ache!

some code used test this:

public void updatescenariowithnochangescauseseftonotpersistchanges() { using ( transactionscope transactionscope = new transactionscope(transactionscopeoption.required, new transactionoptions() { isolationlevel = isolationlevel.readcommitted })) { using (enterprisemodel context = new enterprisemodel()) { //context.configuration.autodetectchangesenabled = false; //did not work //context.configuration.validateonsaveenabled = false; //did not work //((iobjectcontextadapter) context).objectcontext.contextoptions.uselegacypreservechangesbehavior = true; //did not work guid id = new guid("3321dbaf-c55e-e411-80cf-00155d0d70c0"); var efscenario = (from s in context.scenario s.scenarioid == id select s).first(); datetime modifieddatebeforechanges = efscenario.modifieddate; //just setting value itself. efscenario.modifiedby = efscenario.modifiedby; context.savechanges(); //ef scenario object's modifieddate set computed datetime modifieddateafterchanges = efscenario.modifieddate; //there trigger on updates sets modifieddate in database. //also modifieddate set computed column. //the next passes ef4 , fails ef6. //if in ef6 create alter efscenario.modifiedby = efscenario.modifiedby; //then ef6 works! assert.arenotequal(modifieddatebeforechanges, modifieddateafterchanges); } } }

what has worked:

i can alter modifieddate column new value , causes ef observe alter , causes save. trigger updates modified date , because column marked computed, ef retrieves value. (dont like! have go through every method calls savechanges , create sure every entity used updated, beingness updated new modifieddate value.

calling context.entry(efscenario).state = entitystate.modified; before calling savechanges. no improve (1), in opinion. , go 1 now.

my ideal solution: setting can toggle in ef allow me previous behavior of ef4.

bonus question: when did behavior alter in ef? cant seem find document regarding change!

table structure

create table [dbo].[scenario]( [scenarioid] [uniqueidentifier] not null constraint [df_scenario_scenarioid] default (newsequentialid()), [name] [varchar](200) not null, [modifiedby] [varchar](100) not null, [modifieddate] [datetime] not null constraint [df_scenario_modifieddate] default (getdate()), constraint [pk_scenario] primary key clustered ( [scenarioid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go create trigger [dbo].[trupdatemodifieddatescenario] on [dbo].[scenario] instead of update begin set nocount on; update set a.[name]=b.[name] , a.[modifiedby]=b.[modifiedby] , a.[modifieddate]=getdate() [dbo].[scenario] inner bring together inserted b on a.[scenarioid]=b.[scenarioid] end; go

you should able tell ef model changed:

var efscenario = (from s in context.scenario s.scenarioid == id select s).first(); context.entry(efscenario).state = entitystate.modified; context.savechanges(); //should nail database, it's marked modified.

msdn dbcontext.entry()

msdn entitystate

how revert ef4 behavior?

i don't believe there global way revert behavior, going have manually this.

entity-framework entity-framework-4 entity-framework-6

No comments:

Post a Comment