Faling Dutchman
Faling Dutchman

Reputation: 261

Entity Framework table not updating database

My application does something strange, if I execute the following code outside the debugger it does not work, but if I run it with the debugger it works fine: keeping in mind that I step through the code, and not continue the next instant.

As I can gather from the debugger, there is nothing wrong with the code, but maybe there is, I at least cannot find it.

public void Reject(string id)
{
    using (context)
    {
        int intId = Convert.ToInt32(id);
        Hours hr = (from h in context.Hours
                    where h.Id == intId
                    select h).FirstOrDefault();
        hr.Status = 30;
        context.SaveChanges();
    }
}

ApplicationDBContext class

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    static ApplicationDbContext()
    {
        System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
    }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Tasks> Tasks { get; set; }
    public DbSet<Hours> Hours { get; set; }
    public DbSet<OffDays> OffDays { get; set; }
    public static ApplicationDbContext Create()
    {
            return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>()
            .HasRequired(u => u.Department)
            .WithMany(d => d.Users)
            .Map(c => c.MapKey("DepartmentId"));

        modelBuilder.Entity<ApplicationUser>()
            .HasMany(u => u.Supervisors)
            .WithMany();

        modelBuilder.Entity<Department>()
            .HasMany(d => d.Supervisors)
            .WithOptional()
            .Map(c => c.MapKey("SupervisorId"));

        modelBuilder.Entity<Hours>()
            .HasRequired(u => u.UserId)
            .WithMany(h => h.Hours)
            .Map((c => c.MapKey("Hours")));
    }
}

Do you guys have an idea what I can try?

Upvotes: 1

Views: 913

Answers (2)

Bradley Uffner
Bradley Uffner

Reputation: 16991

It looks like you are reusing the same DbContext instance through the life of your application. This goes against Microsoft's recommended best practices, and may introduce some of the odd behavior you are seeing. DbContext manages a LOT of cached data inside each DbContext instance, and keeping it alive for the entire application can cause all sort of craziness as different cached entities are returned. This caching is what I think is causing your problem. Somewhere in your application this instance is getting disconnected from the context, and now that disconnected, cached instance is being returned the next time you ask for it.

The current Entity Framework best practices recommend that you create a new instance of the context for each logical "unit of work".

The easiest way to create a new context instance on demand is like this:

using (var context = new ApplicationDbContext())
{
    //Code here that uses the context
}

Upvotes: 1

mohsen
mohsen

Reputation: 1806

You must first attach the entity that you have retrieved

public void Reject(string id)
{
    using (context)
    {
        int intId = Convert.ToInt32(id);
        Hours hr = (from h in context.Hours
                where h.Id == intId
                select h).FirstOrDefault();
        context.Attach( hr);// here is difference
        hr.Status = 30;
        context.SaveChanges();
   }
}

Upvotes: 0

Related Questions