Concurrency Exception While adding Record

Kmcnet 691 Reputation points
2022-05-05T02:27:50.413+00:00

Hello everyone and thanks for the help in advance. I am using Entity Framework Core in a MVC application to add a record to a Sql database, however I continue to receive the error message "The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded". This particular table is only being accessed by this one application that only adds records. Here is my code:

                using (var ctx = new myDbContext())
                {
                    ctx.tbl_Log_UserActivity.Add(new tbl_Log_UserActivity()
                    {
                        IPAddress = ipAddress,
                        Referer = Referer,
                        UserAgent = userAgent,
                        TimeEntered = DateTime.Now
                    });
                    ctx.SaveChanges();

                }

When this didn't work I tried:

                    var userActivity = new tbl_Log_UserActivity()
                    {
                        IPAddress = ipAddress,
                        Referer = Referer,
                        UserAgent = userAgent,
                        TimeEntered = DateTime.Now
                    };

                    ctx.tbl_Log_UserActivity.Add(userActivity);

                    ctx.SaveChanges();

I receive the same error either way. Any help would be appreciated.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 questions
{count} votes