微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 更新查询需要删除并重新插入所有相关项,这会降低我的应用程序的速度

我有如下数据库结构:

enter image description here

当我更新订单时,我使用以下查询

using (var xaction = new TransactionScope())
{
    foreach (OrderItemDetail orderItemDetail in OrderItemDetailClient.GetAllOrderItemDetails())
    {
        if (orderItemDetail.OrderId == NewOrder.OrderId)
        {
            OrderItemDetailClient.DeleteOrderItemDetail(orderItemDetail);
        }
    }

    IEnumerable<dispatch> dispatches = dispatchClient.GetAlldispatches().Where(x => x.OrderId == NewOrder.OrderId);
    foreach (dispatch dispatch in dispatches)
    {
        foreach (dispatchItemDetail dispatchItemDetail in dispatchItemDetailClient.GetAlldispatchItemDetails().Where(x => x.InvoiceId == dispatch.InvoiceId))
        {
            dispatchItemDetailClient.DeletedispatchItemDetail(dispatchItemDetail);
        }

        dispatchClient.Deletedispatch(dispatch);
    }

    OrderClient.UpdateOrder(NewOrder);

    xaction.Complete();
    TransactionSucceded = true;
}

OrderClient中的UpdateOrder方法如下所示:

public Order UpdateOrder(Order Order)
{
    IOrderRepository OrderRepository = _DataRepositoryFactory.GetDataRepository<IOrderRepository>();

    Order updatedEntity = null;

    if (Order.OrderId == 0)
    {
        updatedEntity = OrderRepository.Add(Order);
    }
    else
    {
        updatedEntity = OrderRepository.Update(Order);
    }

    return updatedEntity;

}

在OrderRepository中:

protected override Order UpdateEntity(RateDifferenceContext entityContext,Order entity)
{
    return (from e in entityContext.OrderSet
            where e.OrderId == entity.OrderId
            select e).FirstOrDefault();
}

然后在DataRepositoryBase类中我使用以下方法

public T Update(T entity)
{
    using (U entityContext = new U())
    {
        T existingEntity = UpdateEntity(entityContext,entity);
        SimpleMapper.PropertyMap(entity,existingEntity);
        entityContext.SaveChanges();
        return existingEntity;
    }
}

删除相关实体时,出现错误:无法更改关系,因为一个或多个外键属性不可为空

我知道手动我不需要在保存订单之前删除orderItemDetails,dispatches和dispatchItemDetails.但如果我不这样做,那么我会得到下面提到的错误

Multiplicity constraint violated

我真的对实体框架不熟悉.任何人都可以建议我保存订单的好方法吗?这样我才能获得速度.

更新:

Where does NewOrder come from? Is it really new?

NewOrder是Order类型的属性,它在viewmodel的构造函数中初始化.以及编辑或保存时.我的意思是如果用户正在创建新订单而不是NewOrder是新订单.如果用户正在编辑现有订单,则NewOrder是现有订单.

What is OrderItemDetailClient. What is dispatchClient?

OrderItemDetailClient是IOrderItemDetailService类型的变量,它也在viewmodel的构造函数中初始化,如下所示:

OrderItemDetailClient = serviceFactory.CreateClient<IOrderItemDetailService>();

其中暗示IOrderItemDetailService是WCF服务实现类.

同样的,

dispatchClient是IdispatchService类型的变量,如下所示:

dispatchClient = serviceFactory.CreateClient<IdispatchService>();

其中暗示IdispatchService是WCF服务实现类.

What’s the role of orderItemDetailList,TotalQuantity,and TotalAmount?

对不起,该代码不是必需的.我想我忘了删除它.

Why do you delete ( and not add) dispatches?

更新订单自动保存Order,OrderItemDetail,dispatch及其相关的dispatchItemDetail.

因此,如果我不删除NewOrder的现有dispatches,那么当我更新Order时,Cascade更新或其他东西认为我已经有一个带有主键的dispatch,我现在正试图存储.所以,我收到一个错误

Multiplicity constraint violated. The role '…' of the 
        relationship '…' has multiplicity 1 or 0..1

The most pressing question is: why don’t you just add the items that are new,delete the ones that are removed and leave the rest alone?

我想我在上面的回答中回答了这个问题,如果我这样做,我在保存更新时会遇到违反Multiplicity约束的错误.

解决方法

问题的很大一部分似乎很可能是您可能从WCF服务中提取大量数据,可能远远超出您的需求.这很昂贵,因为您需要从数据库提取记录,然后将它们序列化,以便将它们从WCF服务中传出(然后在WCF客户端上对它们进行反序列化).

如果您向WCF服务添加了一些方法以仅返回与特定ID相关的数据,那么您可以将返回的数据限制为仅与您尝试执行的操作相关的数据,这会加快速度.例如,而不是

dispatchClient.GetAlldispatches().Where(x => x.OrderId == NewOrder.OrderId);

您可以创建一个名为“GetdispatchesForOrderId”的服务方法,该方法接受订单ID并仅返回与其相关的调度.这将仅发回您需要的数据并且更便宜(即更快).所以上面的行将被替换为

dispatchClient.GetdispatchesForOrderId(NewOrder.OrderId)

权衡是你必须写几个服务方法.

如果您还添加方法“OrderItemDetailClient.GetorderItemDetailsForOrderId”和“dispatchItemDetailClient.GetdispatchItemDetailsForInvoiceId”,那么您的更新代码将变为如下所示:

foreach (OrderItemDetail orderItemDetail in OrderItemDetailClient.GetorderItemDetailsForOrderId(NewOrder.OrderId))
{
    OrderItemDetailClient.DeleteOrderItemDetail(orderItemDetail);
}

foreach (dispatch dispatch in dispatchClient.GetdispatchesForOrderId(NewOrder.OrderId))
{
    foreach (dispatchItemDetail dispatchItemDetail in dispatchItemDetailClient.GetdispatchItemDetailsForInvoiceId(dispatch.InvoiceId))
    {
        dispatchItemDetailClient.DeletedispatchItemDetail(dispatchItemDetail);
    }

    dispatchClient.Deletedispatch(dispatch);
}

OrderClient.UpdateOrder(NewOrder);

如果你不做这样的事情,那么随着数据库的增长,你的问题会随着时间的推移而变得越来越糟 – 每当你要提取的数据量超过你需要的数量时,随着不必要数据的大小增加,所以浪费的时间用于提取您实际不需要的数据.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐