0

I have an issue with my DB context not recognizing changes made to the context for a specific table. CRUD operations on other tables defined in the context are working fine.

For the table that does not recognize updates to the context, I am able to query for data with no problem, but any attempt to change the context has zero effect.

I have confirmed that no update command is coming to SQL Server DB via SQL Profiler, and I added code to the update method to inspect the state of the context object via HasChanges method on the ChangeTracker which always indicates false.

    public async Task<IEnumerable<ScheduleEntry>> UpdateProductionScheduleAsync(ScheduleEntry entry)
    {
        Log.TraceInfo(String.Format("{0} updating CNC Production Schedule Entry", entry.UserName));
        ScheduleEntry result = _context.ProductionSchedule.SingleOrDefault(x => x.Id == entry.Id);
        if (result != null)
        {
            try
            {
                result = entry;

                //BUGBUG: Remove this debugging code
                bool changesMade = _context.ChangeTracker.HasChanges();

                int changeCount = await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Log.TraceError(Log.WarnId.SqlException, String.Format("UpdateProductionScheduleAsync Request Failed: Error:{0}", ex));
            }
        }

        return await GetAllScheduleEntriesAsync();
    }

I am not using the wizard or code first, I just rolled the DB Context class by hand...

[DbConfigurationType(typeof(CodeConfig))]
public class CNCDataContext : DbContext, IDatabaseContext
{
    public CNCDataContext()
        : base("name=CNCProductionData")
    {
        Database.SetInitializer<CNCDataContext>(null);
    }

    public DbSet<RawData> RawData { get; set; }
    public DbSet<ScheduleEntry> ProductionSchedule { get; set; }

    public class CodeConfig : DbConfiguration
    {
        public CodeConfig()
        {
            SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
            SetProviderServices("System.Data.SqlClient",
                System.Data.Entity.SqlServer.SqlProviderServices.Instance);
        }
    }

And the entity definition that the context wont recognize changes for is ...

[Table("ProductionSchedule")]
public class ScheduleEntry
{
    public ScheduleEntry()
    {

    }

    [Key]
    public int Id { get; set; }
    public DateTime DatePartsProduced { get; set; }
    public int Hours { get; set; }
    public string MachNum { get; set; }
    public DateTime StartDate { get; set; }
    public string PartNum { get; set; }
    public int QtyToRun { get; set; }
    public int QtyRan { get; set; }
    public int QtyLost { get; set; }
    public int RedLot { get; set; }
    public int Scrap { get; set; }
    public int Rework { get; set; }
    public int QtyLeftToRun { get; set; }
    public decimal RatePerHour { get; set; }
    public int ShiftsReq { get; set; }
    public string Priority { get; set; }
    public string NextSetup { get; set; }
    public int NextQtyToRun { get; set; }
    public DateTime RefreshTime { get; set; }
    public string UserName { get; set; }
}

How can I debug the context to determine what the problem is with this entity?

  • 1
    You are not updating **anything** inside your method. If you believe that this: `result = entry` will have any effect on the `DbContext` you are wrong. Changes are tracked from entities that are returned *from* the context, entities which are created otherwise are not tracked at all. You need to either map the new values into the entity `result` *or* attach the new entity and set its state to `Modified`. – Federico Dipuma Jun 14 '18 at 18:22
  • Your problem is the line `result = entry;`. Now you have an untracked entity. Solution is to either manually set the properties of result with entry fields (automapper handly for this). Or you need to [move result back into tracking](https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx). See [here](http://kerryritter.com/updating-or-replacing-entities-in-entity-framework-6/) too. – Steve Greene Jun 14 '18 at 18:30
  • Thanks Steve for the clue about tracked/untracked entity. I will dig into that further. I was able to "fix the glitch" by changing the code to _context.Entry(result).CurrentValues.SetValues(entry); – Sean Bennington Jun 14 '18 at 18:50

0 Answers0