c# - object passed to method by reference not maintaining values from DBContext -


basically, i'm passing model object method , in method, assigning proper object database. since reference, assumed persist throughout rest of method in it called/passed. know has proxy of entity framework can't figure out how fix it. here fragment of code:

    [httppost] public actionresult create(newformviewmodel nfvm) {     db = new dbconnection(connstr);     track track= new track();     track parenttrack = new track();       this.create_settrack(nfvm, track, parenttrack);          ... 

and in create_settrack:

        private void create_settrack(newformviewmodel nfvm, track track, track parenttrack)         {             track = db.tracks.firstordefault();             parenttrack = db.tracks.where(i=>i.parentid==track.id).firstordefault();         } 

the track loads in create_settrack then, after code after '...' continues in create, track it's null values.

note method parameter new variable. assign track (the variable) track (the parameter). in method body, parameter overwritten new reference, original track (the variable) has nothing that.

you confused fact changes make same reference object visible outside method body. if you'd set property of new track() object, you'd see value after create_settrack call.

so make method returns tracks, can assign original variables. if internal method, return tuple (using tuples in api methods discouraged, because itemx properties nondescript).

as alternative, assign tracks object (the view model?) not overwritten in method body.

i'd prefer first alternative though. don't methods create side-effects.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -