Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ah, so there could be multiple models which have been modified and might need the DateTime set on save.</p> <p>In which case either push the boolean flags (setDt1 and SetDt2) into the model and when you make changes add those models to a list... or add a Tuple of <code>&lt;model,bool,bool&gt;</code> to a list.</p> <p>Look at this question <a href="https://stackoverflow.com/questions/3265257/getting-all-changes-made-to-an-object-in-the-entity-framework">Getting all changes made to an object in the Entity Framework</a> to see an example of then looping over those stored models to view their modified properties.</p> <p>You can check an entity's <a href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx" rel="nofollow noreferrer">EntityState</a> to get Entities that have been added with </p> <pre><code>var addedStateEntries = Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added); </code></pre> <p>and entities that have been modified with </p> <pre><code>var modifiedStateEntries = Context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified); </code></pre> <p>You can loop over them when you're ready to save and set dt1 or dt2 as appropriate </p> <hr> <p>If I understand correctly then you want dt1 and dt2 to be set to <code>DateTime.Now</code> when you <code>saveChanges()</code></p> <p>In that scenario replace your call to <code>context.SaveChanges()</code> with a call to the below method</p> <pre><code>private void SaveModel(SomeEntities context, SomeModel model bool setDt1, bool setDt2) { DateTime instant = DateTime.Now; if (SetDt1) { model.dt1 = instant; } if (setDt2) { model.dt2 = instant; } context.SaveChanges(); } </code></pre> <hr> <p>So looking at the more complex example... I'd set a flag once I know if the date should be set on save. I've left <code>SavingDateTimeNow()</code> in place as I don't know if it does other tasks as well as setting dt1 to Now</p> <pre><code>var context = new SomeEntities(); SomeModel model = context.GetSomeModel(...); var setDt1 = false; var setDt2 = false; // SomeModel has two DateTime fields: dt1 and dt2 if(someComplexCondition) { model.dt1 = SavingDateTimeNow(); // .... 13:42:00 setDt1 = true; } // some code from which I would know if someOtherComplexCondition is true if(someOtherComplexCondition) { model.dt2 = SavingDateTimeNow(); // .... 13:42:10 setDt2 = true; } // again 10s. code // much more code here SaveModel(context, model, setDt1, setDt2); // .... 13:42:20 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload