How to correctly override SaveChanges function in EF 4.1

I need to automatically insert the current date when I create a new order in my EF 4.1 MVC3 web application. I also need to automatically add the user who is currently logged in - I use forms-based authentication. After searching the Internet, I found that I needed to override the SaveChanges () function, and an article on StackOverflow showed how to do this: Entity Framework 4.1 Automatic Date . However, I could not get the code to work. Where does this code go? How to find out what is?

Sorry if the question is trivial - I'm new to EF. B.

+5
source share
1 answer

,

public partial class YourDbContext: DbContext
    {
      public  YourDbContext()
        {               
        }
      public override int SaveChanges()
      {
       // do your additional stuff here.. (Ex:- save current user)
           return base.SaveChanges();
      }   
}

- ,

   Public class YourEntity{

      public YourEntity(){
          CreatedDate=DateTime.Now;
      }

      Public DateTime CreatedDate{get;set;}
   }
+4

All Articles