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?
contextan instance field? Do you set it via the constructor? Do you callRejector other methods multiple times for the same instance of the containing class?AttachDBFileNamein the connection string?bin/debugfolder which always gets overwrittend when you hit F5. Why are you using a file-based database anyway? They don't scale, they are deprecated and they will be removed in a future versions. Check Aaron Bertrand's article on why you shouldn't use themconnectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-something.mdf;Initial Catalog=aspnet-aspnet-something;That is how I declaired it, I assume that is the correct connectionstring.