0

尝试调用.SaveChanges()实体框架时出现奇怪的错误。

我正在尝试保存Order这样的

public void SaveOrder(UserDTO user, ArticleDTO article, PriceDTO price, decimal amount)
{
    //Get the order with type cart. If no order exist create a new order with type cart.
    var order = _orderRepository.GetCartOrderForCustomer(user.Customer.CustomerREFID);

    if (order == null)
    {
        order = new Order()
        {
            CustomerREFID = user.Customer.CustomerREFID,
            CreateDate = DateTime.Now,
            OrderType = OrderType.Cart
        };

        _orderRepository.Add(order);
        _orderRepository.UnitOfWork.Commit();
    }
}

当我只调用这个方法时,肯定它工作正常。但是当我在这个方法之前调用另一个方法时,我会得到错误。

before 方法只是获取文章和价格。

public IEnumerable<Article> GetArticlesByCategory(int categorySection, int headCategory, string customerREFID)
{
    var currentUnitOfWork = this.UnitOfWork as MainBCUnitOfWork;

    //Linq query without keys.
    var result = (from a in currentUnitOfWork.Articles
                  join cat in currentUnitOfWork.Categories on a.CategoryID equals cat.CategoryID
                  join cf in currentUnitOfWork.CategoryReferences on cat.ID equals cf.CategoryID
                  join c in currentUnitOfWork.Customers on a.Lagstalle equals c.LagStalle
                  where cf.RefID == categorySection && cat.HuvudKat == headCategory && c.CustomerREFID == customerREFID
                  select a).ToList();

    var artnumbers = result.Select(a => a.Number).ToList();

    var prices = currentUnitOfWork.Prices.Where(p => artnumbers.Contains(p.ArtNr) && p.FtgNr == customerREFID).ToList();

    Parallel.ForEach(result, a =>
    {
        a.Prices = prices.Where(p => p.ArtNr == a.Number).ToList();
    });

    return result.ToList();

}

所以打电话时SaveOrder我得到这个错误:

{System.Data.SqlClient.SqlException (0x80131904):违反主键约束“PK_dbo.PriceArticles”。无法在对象“dbo.PriceArticles”中插入重复键。重复键值为 (6653, 1)。该语句已终止。在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔 breakConnection,操作1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject state&Obj, Boolean dataReady) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task , Boolean asyncWrite) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream,字符串方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 完成,String methodName,Boolean sendToPipe,Int32 timeout,Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator 翻译器,EntityConnection 连接,字典2 identifierValues, List1 generatedValues) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter 适配器) ClientConnectionId:dfc62e28-3751-4a54-89f4-5fa8195cab2a}

这个表和get 表无关Order?为什么我只添加新的 Entity Framework 会提交其他表Order

我该如何解决这个问题?

4

0 回答 0