none
Code first - overriding DbContext.SaveChanges() RRS feed

  • Question

  • Hi all,

    We are looking ito entering EntityFramework with our app. The EF would provide read logic as is but we would have to completly replace save changes logic. Initially, we looked into overriding DataContext.SaveChanges() and found out we can get all the required change info via ObjectStateManager.

    Now, we are looking at code first and DbContext and we found out that we can override SaveChanges() but we have no access to the wrapped .InternalContext.ObjectContext. Is there a way around this? (would reflection be a reasonable option?)

    (I get the feeling MS provided override for SaveCahnges just for the sake of a better OnSaveChanges and not as a way for replacing the whole logic)

    Thanks,

    Asher

     

    Wednesday, March 16, 2011 11:43 AM

Answers

  • Good to see you again, Asher!

    I started to use EF 4.1 new features just recently, so I am not sure I can provide the best practice.  However, you can refer to this method by using DbContext.ChangeTracker.Entries() to retrieve the necessary information.

    ==============================================================================
        public class MyContext : DbContext
        {
            public DbSet<User> Users { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>()
                    .HasKey(u => u.UserID);
                modelBuilder.Entity<User>()
                    .Property(u => u.UserID).IsRequired();
            }

            public override int SaveChanges()
            {
                var modified = this.ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Modified);
                return base.SaveChanges();
            }
        }

        public class User
        {
            public int UserID { get; set; }
            public string Name { get; set; }
        }
    ==============================================================================

    Test codes:
    ==============================================================================
                using (var db = new MyContext())
                {
                    var users = db.Users.ToList();
                    var user = users.First();
                    user.Name = "New Name";
                    db.SaveChanges();
                }
    ==============================================================================

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    • Marked as answer by Asher_barak Monday, March 21, 2011 7:46 PM
    Thursday, March 17, 2011 9:30 AM
    Moderator

All replies

  • Good to see you again, Asher!

    I started to use EF 4.1 new features just recently, so I am not sure I can provide the best practice.  However, you can refer to this method by using DbContext.ChangeTracker.Entries() to retrieve the necessary information.

    ==============================================================================
        public class MyContext : DbContext
        {
            public DbSet<User> Users { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>()
                    .HasKey(u => u.UserID);
                modelBuilder.Entity<User>()
                    .Property(u => u.UserID).IsRequired();
            }

            public override int SaveChanges()
            {
                var modified = this.ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Modified);
                return base.SaveChanges();
            }
        }

        public class User
        {
            public int UserID { get; set; }
            public string Name { get; set; }
        }
    ==============================================================================

    Test codes:
    ==============================================================================
                using (var db = new MyContext())
                {
                    var users = db.Users.ToList();
                    var user = users.First();
                    user.Name = "New Name";
                    db.SaveChanges();
                }
    ==============================================================================

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    • Marked as answer by Asher_barak Monday, March 21, 2011 7:46 PM
    Thursday, March 17, 2011 9:30 AM
    Moderator
  • Hi Asher,

    Could you please let us know how's the problem now?  If you need any further assistance, please feel free to let us know.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Monday, March 21, 2011 1:14 AM
    Moderator
  • Hi Lingzhi,

    Good to hear you too.

    I got the ChangeTracker.Entries() and it does help. Thanks.

    Asher

    Monday, March 21, 2011 7:46 PM
  • It's my pleasure!  :)

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Tuesday, March 22, 2011 1:23 AM
    Moderator