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?