简体   繁体   English

实体框架数据库更改未在DbContext中看到

[英]Entity Framework database changes not seen in the DbContext

I have a simple application that is connected to a database (Entity Framework, code first). 我有一个连接到数据库的简单应用程序(实体框架,代码优先)。 I have a screen that is used to update an entry to the database. 我有一个用于更新数据库条目的屏幕。 I am able to update the database with the following code: 我可以使用以下代码更新数据库:

public void UpdatePlayer()
{
    Player playerInDb = GetPlayerFromDb(_player.Id);

    playerInDb.Name = Name;
    playerInDb.Score = Score;

    _context.SaveChanges();
    TryClose();
}

private Player GetPlayerFromDb(int id)
{
    return _context.Players.Single(p => p.Id == id);
}

If I refresh the database immediately after calling SaveChanges, I can see the changes in the actual database, but if I look at _context.Players, the old values are still there. 如果在调用SaveChanges之后立即刷新数据库,则可以看到实际数据库中的更改,但是如果查看_context.Players,旧值仍然存在。 It is only after I close my app and reopen it that the changes appear in the DbContext. 只有在我关闭我的应用程序并重新打开它之后,更改才会出现在DbContext中。 How can I make sure the updated data from the database appears on my DbContext after calling SaveChanges? 如何在调用SaveChanges之后确保来自数据库的更新数据出现在DbContext上?

UPDATE: 更新:

I've changed my code to the following: 我将代码更改为以下内容:

    public void UpdatePlayer()
    {
        Player playerInDb = GetPlayerFromDb(_player.Id);

        playerInDb.Name = Name;
        playerInDb.Score = Score;

        using (var context = new ApplicationDbContext())
        {
            context.Players.Attach(playerInDb);
            context.Entry(playerInDb).State = EntityState.Modified;
            context.SaveChanges();
        }
        TryClose();
    }

I still have the same issue. 我仍然有同样的问题。 The record is updated in the db but not in the application, so my UI does not get updated with the new data. 该记录在数据库中更新,但在应用程序中未更新,因此我的UI不会使用新数据进行更新。 Even if I reload the entire collection of players from the db, nothing gets updated until I completely relaunch the application. 即使我从数据库重新加载了整个播放器集合,也没有任何更新,直到我完全重新启动该应用程序为止。

You will need an extra step to modify the object: 您将需要一个额外的步骤来修改对象:

_context.Players.Attach(playerInDb);

var entry = _context.Entry(playerInDb);
entry.State = EntityState.Modified;

_context.SaveChanges();   

Also you may need to check these for more details: 另外,您可能需要检查以下详细信息:
- https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx -https://msdn.microsoft.com/zh-CN/library/jj592676(v=vs.113).aspx
- Entity Framework 5 Updating a Record - 实体框架5更新记录
- How to update record using Entity Framework 6? - 如何使用Entity Framework 6更新记录?

Entity by itself has a mechanism to cache the entities to increase the speed time when select etc are done twice or more than one time. 实体本身具有一种机制,可以在两次选择或多次执行选择等操作时缓存实体,以增加速度。 You can change the state of an entity by setting. 您可以通过设置来更改实体的状态。

entity.State = EntityState.Modified;

Please consider using unit of work concept rather than sharing the same dbContext inside the class. 请考虑使用工作单元概念,而不是在类内部共享相同的dbContext This will dispose the context itself by using this syntax: 这将使用以下语法来处理上下文本身:

using(DbContext dbContext = new DbContext())
{
    //Do some stuff, update, delete etc
}

If you don`t dispose the context properly you end up causing other problems for application as well. 如果您没有适当地处理上下文,那么最终还会导致应用程序出现其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM