I'm using Entity Framework 6.1.1 and when I use a custom model binder in an async call it throw me an exception because the child entity (Brand) is not loaded and mandatory. I'm using ninject, I updated to the latest version ( 3.2.2.0) Here's the code :
public class PlayerModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var context = DependencyResolver.Current.GetService<DbContext>();
var id = ModelBinderHelpers.GetA<Guid>(bindingContext, "id");
if (id == null || !id.HasValue)
throw new HttpException(404, "Not found");
var entity = context.Set<Player>().Find(id);
if (entity == null)
throw new HttpException(404, "Not found");
return entity;
}
}
This will work :
public ActionResult Deactivate(Player player)
{
try
{
player.Deactivate();
DbContext.SaveChanges();
}
catch (Exception ex)
{
var b = ex;
}
FlashBag.AddMessage(
FlashType.Success,
Strings.Player_DeactivatedSuccessfully);
return RedirectToAction("Index");
}
But this does not work :
public async Task<ActionResult> Deactivate(Player player)
{
try
{
player.Deactivate();
await DbContext.SaveChangesAsync();
}
catch (Exception ex)
{
var b = ex;
}
FlashBag.AddMessage(
FlashType.Success,
Strings.Player_DeactivatedSuccessfully);
return RedirectToAction("Index");
}
Thanks for the help!