asp.net-mvc context.SaveChanges 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8056761/
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-09-08 01:35:03  来源:igfitidea点击:

context.SaveChanges not working

asp.net-mvcasp.net-mvc-3entity-frameworkentity-framework-4.1

提问by DotnetSparrow

My update method is not working in an ASP.NET MVC 3 application. I have used the following EF 4.1 code:

我的更新方法在 ASP.NET MVC 3 应用程序中不起作用。我使用了以下 EF 4.1 代码:

[HttpPost]
public ActionResult UpdateAccountDetails(Account account)
{
    if (ModelState.IsValid)
    {
        service.SaveAccount(account);
    }
}

and SaveAccountlooks like this:

SaveAccount看起来像这样:

internal void SaveAccount(Account account) {         
    context.SaveChanges();
}

回答by Slauma

internal void SaveAccount(Account account) {

    // Load current account from DB
    var accountInDb = context.Accounts.Single(a => a.Id == account.Id);

    // Update the properties
    context.Entry(accountInDb).CurrentValues.SetValues(account);

    // Save the changes
    context.SaveChanges();
}

Alternative:

选择:

internal void SaveAccount(Account account) {

    context.Entry(account).State = EntityState.Modified;
    context.SaveChanges();
}

回答by Erik Funkenbusch

The problem here is that you're not accounting for the fact that Web pages are stateless. You probably pupulate your page with the account data returned from the database, but then the object is destroyed at the end of the request.

这里的问题是您没有考虑到 Web 页面是无状态的这一事实。您可能使用从数据库返回的帐户数据填充页面,但是该对象在请求结束时被销毁。

On postback, a new Acccount object is created by the model binder, but this one is not hooked up to the database, so your database context has no idea that it even exists. So when you call SaveChanges, nothing has changed as far as it is concerned.

在回发时,模型绑定器创建了一个新的 Acccount 对象,但是这个对象没有连接到数据库,因此您的数据库上下文甚至不知道它是否存在。因此,当您调用 SaveChanges 时,就它而言,没有任何变化。

You have to either get a new Account object from the database and update it's fields with the data from the model binder created Account, or attach the new account object to the database.

您必须从数据库中获取一个新的 Account 对象并使用模型绑定器创建的 Account 中的数据更新它的字段,或者将新的 account 对象附加到数据库。

回答by msmucker0527

This article should help

这篇文章应该有帮助

http://msdn.microsoft.com/en-us/library/bb896271.aspx

http://msdn.microsoft.com/en-us/library/bb896271.aspx

You may need to add context.Accounts.Attach(account);to reattach your entity to the context

您可能需要添加context.Accounts.Attach(account);以将您的实体重新附加到上下文

回答by Yakimych

You aren't making any changes, so there is really nothing to be saved. The simplest way may be doing the following:

您没有进行任何更改,因此实际上没有什么可保存的。最简单的方法可能是执行以下操作:

internal void SaveAccount(Account account)
{
    context.Attach(account);
    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(account);
    entry.SetModified();

    context.SaveChanges();
}