C# 实体框架 SaveChanges() 不更新数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14857813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:14:33  来源:igfitidea点击:

Entity Framework SaveChanges() not updating the database

c#entity-framework

提问by Sachin Kainth

var paymentAttempt = _auctionContext.PaymentAttempts.Where(o => o.Id == paymentAttemptId).SingleOrDefault();
if (paymentAttempt != null)
{
    paymentAttempt.PaymentAttemptStatusId = (int)PaymentAttemptStatus.Defunct;
    paymentAttempt.PaymentAttemptStatus = _auctionContext.PaymentAttemptStatuses.Where(pas => pas.Id == paymentAttempt.PaymentAttemptStatusId).First();

    var relevantWinningBidsTotalPrices = _auctionContext.GetWinningBidsTotalPricesForPaymentAttempt(paymentAttemptId).ToArray();

    foreach (var winningBid in relevantWinningBidsTotalPrices)
    {
        winningBid.Locked = false;
        _auctionContext.UpdateObject(winningBid);
    }
    _auctionContext.SaveChanges();
}

In the above code after

在上面的代码之后

_auctionContext.SaveChanges();

is called winningBidis updated as expected but paymentAttemptisn't. Why is this? It is really frustrating. There is no error either. I would expect a failure to occur if there was a problem like EF wasn't tracking the object or something like that, but no such error is happening.

被调用winningBid按预期更新但paymentAttempt不是。为什么是这样?这真的很令人沮丧。也没有错误。如果出现诸如 EF 未跟踪对象或类似问题之类的问题,我希望会发生故障,但没有发生此类错误。

采纳答案by Jorge

That's because you need to pass the paymentAttemptobject to your context, to let it know that it is an object that needs to be updated.

那是因为您需要将paymentAttempt对象传递给您的上下文,让它知道它是一个需要更新的对象。

For example, assuming that _auctionContextis an instance of DbContext:

例如,假设这_auctionContext是一个实例DbContext

// any changes related to the paymentAttempt object 

_auctionContext.Entry(paymentAttempt).State = EntityState.Modified;

foreach (var winningBid in relevantWinningBidsTotalPrices)
{
   winningBid.Locked = false;
   _auctionContext.UpdateObject(winningBid);
}

_auctionContext.SaveChanges();

Another option is the Attachmethod:

另一种选择是Attach方法:

_auctionContext.Attach(paymentAttempt);
_auctionContext.ObjectStateManager.ChangeObjectState(paymentAttempt, System.Data.EntityState.Modified);

回答by smirkingman

I fell on this question but for a different problem. I discovered that if you call SaveChanges() on an object that hasn't been modified, EF will not update anything. This makes sense, but I needed the DB to be updated so that other users would see that a SaveChanges() had been executed, regardless of whether any fields had changed. To force an update without changing any fields:

我陷入了这个问题,但有一个不同的问题。我发现如果你在一个没有被修改的对象上调用 SaveChanges(),EF 不会更新任何东西。这是有道理的,但我需要更新数据库,以便其他用户可以看到 SaveChanges() 已被执行,无论是否有任何字段已更改。要在不更改任何字段的情况下强制更新:

    Dim entry As DbEntityEntry = entities.Entry(myentity)
    entry.State = Entity.EntityState.Modified

回答by borzou27

If you don't have Entrytry adding:

如果您没有Entry尝试添加:

using System.Data.Entity.Infrastructure;

using System.Data.Entity;

then you may simply use:

那么你可以简单地使用:

_auctionContext.Entry(paymentAttempt).State = EntityState.Modified;
_auctionContext.SaveChanges();