简体   繁体   English

EF中的自定义模型活页夹不会在异步中加载子实体

[英]Custom model binder in EF do not load child entities in Async

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. 我正在使用Entity Framework 6.1.1,当我在异步调用中使用自定义模型绑定程序时,由于未加载子实体(品牌)且该子实体(品牌)是强制性的,因此抛出异常。 I'm using ninject, I updated to the latest version ( 3.2.2.0) Here's the code : 我正在使用ninject,我已更新到最新版本(3.2.2.0),这是代码:

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! 谢谢您的帮助!

After reading this post 看完这篇文章

I ended up doing this : 我最终这样做:

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>()
                .Include(x => x.Brand)
                .FirstOrDefault(x => x.Id == id);

            if (entity == null)
                throw new HttpException(404, "Not found");

            return entity;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM