实体框架崩溃

发布于 2024-10-22 00:43:37 字数 1691 浏览 1 评论 0原文

我正在开发一个 MVC 应用程序,但我不清楚保存这些实体的理想方法。我现在所做的事情是有效的,但至少可以说是令人讨厌的。

我的操作方法采用 ViewModel 并使用 Automapper 将它们映射到相关实体。我的实体是请求者、订单和订单详细信息。

显然,所有这些对 SaveChanges 方法的调用都是错误的,但我遇到了添加新子项而不是更新其他项的问题。

任何有关此代码的外观的帮助将不胜感激。

贾森·麦肯齐

 [HttpPost]
    public ActionResult Edit(FormCollection formValues, RequestorViewModel requestor, OrderViewModel order, List<OrderDetailViewModel> OrderDetails)
    {

        var query = from r in db.Requestors
                    where r.RequestorID == requestor.RequestorID
                    select r;
        var req = query.SingleOrDefault();

        var orderQuery = from o in db.Orders
                         where o.RequestorID == requestor.RequestorID
                         select o;
        var or = orderQuery.SingleOrDefault();


        List<OrderDetail> orDet = db.OrderDetails.Where(od => od.OrderID == or.OrderID).ToList();


        Mapper.CreateMap<RequestorViewModel, Requestor>();
        req = Mapper.Map<RequestorViewModel, Requestor>(requestor);

       Mapper.CreateMap<OrderViewModel, Order>();
       or = Mapper.Map<OrderViewModel, Order>(order);

       Mapper.CreateMap<OrderDetailViewModel, OrderDetail>();
       orDet = Mapper.Map<List<OrderDetailViewModel>, List<OrderDetail>>(OrderDetails);

       foreach (OrderDetail od in orDet)
       {
           db.OrderDetails.ApplyCurrentValues(od);
           db.SaveChanges();

       }

        db.Requestors.ApplyCurrentValues(req);
        db.SaveChanges();

        db.Orders.ApplyCurrentValues(or);
        db.SaveChanges();



        return View("Index");
    }

I am developing an MVC application which and I'm not clear on the ideal way to save these entities. What I am doing now works but is nasty to say the least.

My action method takes ViewModels and uses Automapper to map them to the related entities. My entities are Requestor, Order and OrderDetail.

Obviously all these calles to the SaveChanges method are wrong but I have been running into issues of new children being added instead of updated amongst other things.

Any help on how this code should look would really be appreciated.

Jason MacKenzie

 [HttpPost]
    public ActionResult Edit(FormCollection formValues, RequestorViewModel requestor, OrderViewModel order, List<OrderDetailViewModel> OrderDetails)
    {

        var query = from r in db.Requestors
                    where r.RequestorID == requestor.RequestorID
                    select r;
        var req = query.SingleOrDefault();

        var orderQuery = from o in db.Orders
                         where o.RequestorID == requestor.RequestorID
                         select o;
        var or = orderQuery.SingleOrDefault();


        List<OrderDetail> orDet = db.OrderDetails.Where(od => od.OrderID == or.OrderID).ToList();


        Mapper.CreateMap<RequestorViewModel, Requestor>();
        req = Mapper.Map<RequestorViewModel, Requestor>(requestor);

       Mapper.CreateMap<OrderViewModel, Order>();
       or = Mapper.Map<OrderViewModel, Order>(order);

       Mapper.CreateMap<OrderDetailViewModel, OrderDetail>();
       orDet = Mapper.Map<List<OrderDetailViewModel>, List<OrderDetail>>(OrderDetails);

       foreach (OrderDetail od in orDet)
       {
           db.OrderDetails.ApplyCurrentValues(od);
           db.SaveChanges();

       }

        db.Requestors.ApplyCurrentValues(req);
        db.SaveChanges();

        db.Orders.ApplyCurrentValues(or);
        db.SaveChanges();



        return View("Index");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2024-10-29 00:43:38

首先,您可以相当多地清理您的查询。

var req = (from r in db.Requestors
                where r.RequestorID == requestor.RequestorID
                select r).SingleOrDefault();

var or = (from o in db.Orders.Include("OrderDetails")
                     where o.RequestorID == requestor.RequestorID
                     select o).SingleOrDefault();

了解 .Inlude 如何映射所有相关数据。这非常有用。

您也应该只需要调用 SaveChanges() 一次。

First you can clean up your queries quite a bit.

var req = (from r in db.Requestors
                where r.RequestorID == requestor.RequestorID
                select r).SingleOrDefault();

var or = (from o in db.Orders.Include("OrderDetails")
                     where o.RequestorID == requestor.RequestorID
                     select o).SingleOrDefault();

Take a look at how .Include works to map all your related data. It's very useful.

You should only need to call SaveChanges() once too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文