I am hosting a .Net Web Api on azure, the api is just updating one row in a sql server database. Here is my Updatecode:
using (Storage ctx = new Storage())
{
string json = JsonConvert.SerializeObject(ev, _jsonsettings);
Data ex = ctx.dbsData.FirstOrDefault();
if (ex == null)
ctx.dbsData.Add(new Data() { Json = json, LastUpdate = DateTime.Now });
else
{
ex.Json = json;
ex.LastUpdate = DateTime.Now;
ctx.Entry(ex).State = EntityState.Modified;
}
ctx.SaveChanges();
return new HttpResponseMessage(HttpStatusCode.OK);
}
and i have a second method which only clears the table:
[HttpDelete]
public HttpResponseMessage Clear()
{
using (Storage ctx = new Storage())
{
ctx.dbsData.RemoveRange(ctx.dbsData);
ctx.SaveChanges();
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
See also my DbContext Class:
public class Storage : DbContext
{
public DbSet<Data> dbsData { get; set; }
public Storage(string connectionstring) : base(connectionstring)
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
Configuration.ValidateOnSaveEnabled = false;
Configuration.AutoDetectChangesEnabled = false;
}
public Storage() : this("tom")
{
}
}
My Problem is, that both methods are extremly slow. I found out if i remove the SaveChanges() Command it executes way faster. So my question is how can i improve performance and why does it take that long to update/delete one single row?