简体   繁体   English

实体框架 - 如何正确更新?

[英]Entity Framework - how do I update correctly?

I get the following error: https://i.imgur.com/d13B1vi.png我收到以下错误: https://i.imgur.com/d13B1vi.png

My system: on server startup, I load all database accounts into a list Accounts_ .我的系统:在服务器启动时,我将所有数据库帐户加载到列表Accounts_中。

Every 5 minutes I want to update all accounts in the database with updated Accounts_ entries.每 5 分钟我想用更新的Accounts_条目更新数据库中的所有帐户。

So I change for example the level from an entry in Accounts_ and want to sync it to the database.因此,例如,我从Accounts_中的条目更改级别,并希望将其同步到数据库。 Currently I get the error shown above, but second point is that that update all accounts - but I want to update all that have been changed.目前我收到上面显示的错误,但第二点是更新所有帐户 - 但我想更新所有已更改的内容。

My code: https://i.imgur.com/MYtDXpb.png我的代码: https://i.imgur.com/MYtDXpb.png

Code:代码:

internal static async void SaveAllAccounts()
    {
        try
        {
            using (var db = new gtaContext())
            {
                foreach (var accounts in Model.ServerAccounts.Accounts_.ToList())
                {
                    db.Entry(accounts).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    db.Accounts.Update(accounts);
                }
                await db.SaveChangesAsync();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"{e}");
        }
    }

sometimes "Connection not opened"有时“连接未打开”

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

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