Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a project used Entity Framework, every table or entity has a same two columns "created_date and modified_date", i want to fill both properties before inserting entity object or update it.

how can do it?
Posted
Comments
JoCodes 13-Jan-14 1:56am    
Can you make it bit clear ? If you have 2 columns for every table for dates then why trying to insert it "before"???
Sandeep Singh Shekhawat 13-Jan-14 2:38am    
Why are you not defining triggers (Insert / Update) on these tables in database?

Try like this..



C#
public void Create<T>(T obj)
   {
       var created_date = typeof(T).GetProperty("created_date");
       var modified_date = typeof(T).GetProperty("modified_date");
       created_date.SetValue(obj, DateTime.Now, null); // it will saved with current time
       modified_date.SetValue(obj, null, null); // it will be null on first time
   }
   public void Modify<T>(T obj)
   {
       var created_date = typeof(T).GetProperty("created_date");
       var modified_date = typeof(T).GetProperty("modified_date");
       created_date.SetValue(obj, created_date.GetValue(obj, null), null); // it will saved with created time
       modified_date.SetValue(obj, DateTime.Now, null); // it will saved with current time
   }
 
Share this answer
 
Whenever you 'insert' the value in DB first time it should go in created date column with the default Datetime value either from code or from direct DB. Do same when update no need to do this before insert. For more reference you can check this example.

http://stackoverflow.com/questions/6677160/in-entity-framework-how-to-call-a-method-on-entity-before-saving[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900