In Entity framework, how to call a method on Entity before saving?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

I have an almost identical situation and I manage it by handling the SavingChanges event on the context First I create an interface that defines the timestamping operations: public interface IHasTimeStamp { void DoTimeStamp(); } Then I implement this interface in my entities: Public class User : IHasTimeStamp ( public void DoTimeStamp() { if(dataContext. Entry(this). State == System.Data.EntityState.

Added) { //add creation date_time this. CreationDate=DateTime. Now; } if(dataContext.

Entry(this). State == System.Data.EntityState. Modified) { //update Updation time this.

UpdatedOnDate=DateTime. Now; } } } The final step is to register the SavingChanges handler and implement it public partial class MyEntities { partial void OnContextCreated() { // Register the handler for the SavingChanges event. This.

SavingChanges += new EventHandler(context_SavingChanges); } // SavingChanges event handler. Private static void context_SavingChanges(object sender, EventArgs e) { // Validate the state of each entity in the context // before SaveChanges can succeed. Foreach (ObjectStateEntry entry in ((ObjectContext)sender).

ObjectStateManager. GetObjectStateEntries(EntityState. Added | EntityState.

Modified)) { if (!entry. IsRelationship && (entry. Entity is IHasTimeStamp)) { (entry.

Entity as IHasTimeStamp).DoTimeStamp(); } } } }.

I have an almost identical situation and I manage it by handling the SavingChanges event on the context. First I create an interface that defines the timestamping operations: public interface IHasTimeStamp { void DoTimeStamp(); } Then I implement this interface in my entities: Public class User : IHasTimeStamp ( public void DoTimeStamp() { if(dataContext. Entry(this).

State == System.Data.EntityState. Added) { //add creation date_time this. CreationDate=DateTime.

Now; } if(dataContext. Entry(this). State == System.Data.EntityState.

Modified) { //update Updation time this. UpdatedOnDate=DateTime. Now; } } } The final step is to register the SavingChanges handler and implement it.

Public partial class MyEntities { partial void OnContextCreated() { // Register the handler for the SavingChanges event. This. SavingChanges += new EventHandler(context_SavingChanges); } // SavingChanges event handler.

Private static void context_SavingChanges(object sender, EventArgs e) { // Validate the state of each entity in the context // before SaveChanges can succeed. Foreach (ObjectStateEntry entry in ((ObjectContext)sender). ObjectStateManager.

GetObjectStateEntries(EntityState. Added | EntityState. Modified)) { if (!entry.

IsRelationship && (entry. Entity is IHasTimeStamp)) { (entry. Entity as IHasTimeStamp).DoTimeStamp(); } } } }.

Good job! Upvote. Could have been my answer and the way to go.

– Joep Greuter Jul 13 at 19:48.

I have an almost identical situation and I manage it by handling the SavingChanges event on the context.

I am implementing IValidatableObject interface to make this entity SelfValidating. Iam planning to shift u. Now; code inside User class.

My Question: is there any existing interface that provides some method that gets called before Save and After Validate or some other ways of doing this, that I may not be knowing.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions