I have a simple app which performs standard CRUD operations.
My issue is that at the moment, it does not seem to be editing values in the database. I have debugged through the process and seen that it fails on the set.Attach(entity) line in the context.
Model
[Table("RepackRequest")]
public partial class RepackRequest
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public RepackRequest()
{
}
public int ID { get; set; }
[Required]
public string FromItem { get; set; }
[Required]
public string ToItem { get; set; }
public int Quantity { get; set; }
public int? QuantityCompleted { get; set; }
[Column(TypeName = "date")]
public DateTime DateRequested { get; set; }
[Required]
public string RequestedBy { get; set; }
[Required]
public string RequestedByEmail { get; set; }
public string ActionRequired { get; set; }
[Required]
public string Division { get; set; }
[Required]
[StringLength(1)]
public string Status { get; set; }
[Column(TypeName = "date")]
public DateTime? CompletionDate { get; set; }
[Required]
public string Customer { get; set; }
[Required]
public string Priority { get; set; }
[Required]
public string EnteredBy { get; set; }
public string CompletedBy { get; set; }
public string FromLocation { get; set; }
public string ToLocation { get; set; }
public string ReworkedBy { get; set; }
public string OriginalDriver { get; set; }
public string FromItemDriver { get; set; }
public string FromLocationDriver { get; set; }
public string ToLocationDriver { get; set; }
public string Workforce { get; set; }
[Required]
public string OrderNum { get; set; }
[Column(TypeName = "date")]
public DateTime PlannedCompletion { get; set; }
[Column(TypeName = "date")]
public DateTime? StartDate { get; set; }
}
API Controller Action
[HttpPost, Route("api/Repack/Update")]
public async Task<HttpResponseMessage> UpdateRepack([FromBody] RepackRequest repack)
{
var oldStatus = _uow.RepackService.Get(repack.ID).Status;
_uow.RepackService.UpdateRepack(repack);
_uow.Save();
if (repack.Status == "C")
await _helper.SendCompletedRepackEmail(repack);
else if (repack.Status != oldStatus)
await _helper.SendStatusChangeEmail(repack, oldStatus);
return Request.CreateResponse(HttpStatusCode.OK, repack.ID);
}
Service Method
public void UpdateRepack(RepackRequest repack)
{
_context.SetModified(repack);
_context.Save(); //_context.SaveChanges() called inside here
}
Context Method
public void SetModified<T>(T entity) where T : class
{
var set = Set<T>();
set.Attach(entity); //FAILS HERE
Entry(entity).State = EntityState.Modified;
}
I have checked that the ID etc of the object is filled so that Entity Framework can find the existing record and im now out of ideas.
I dont get any error messages at all. All i see is that once it tries to attach the entity, it goes to the Dispose() method of my UnityResolver.
Any help would be greatly appreciated.
Thanks!!!!