0

I get the following error: https://i.stack.imgur.com/DMgyO.png

My system: on server startup, I load all database accounts into a list Accounts_.

Every 5 minutes I want to update all accounts in the database with updated Accounts_ entries.

So I change for example the level from an entry in Accounts_ and want to sync it to the database. 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.stack.imgur.com/sKb23.png

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • how do you invoke that method? – Jakub Kozera May 18 '20 at 18:20
  • I have not seen this exception before. Usual issue with Entity you made changes to the Database tables or classes in c# and then did not refresh the mapping file (EDMX). The EDMX file are the rules the link the database tables to the c# classes. The linking has to be correct for the code to run. See following : https://stackoverflow.com/questions/2947511/entity-framework-how-do-you-refresh-the-model-when-the-db-changes – jdweng May 18 '20 at 18:23
  • please post the actual code and error message as formatted text in the question, not as image. See https://meta.stackoverflow.com/a/285557/479251 – Pac0 May 18 '20 at 18:50
  • Consider moving the SaveChangesAsync() call outside of the loop. – Alexander Ryan Baggett May 18 '20 at 18:56

1 Answers1

0

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"