Entity Framework Core 3.1.7 : failure during Update of record

Viewed 524

I have the most simple ASP.NET Core 3.1 Web API solution, with a model project that uses Entity Framework Core 3.1.7 to map to my models to SQL Server.

I have the following entity:

[Table("SessionDetails")]
public class SessionDetailsModel
{
    public long Id { get; set; }
    public DateTime CreateDate { get; set; }
    public Guid Token { get; set; }
    public bool IsValid { get; set; }
}

which is created each time a user calls the Login endpoint (POST).

The db context is configured the following way:

public class RunMeDbContext : DbContext
{
    public RunMeDbContext(DbContextOptions<RunMeDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SessionDetailsModel>()
            .HasKey(x => new {x.Id, x.CreateDate});

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<SessionDetailsModel> SessionDetails { get; set; }
}

The insertion succeeds fine, but then when I call the logout endpoint which gets the previously created SessionDetails record by the token field, and tries to update the 'IsValid' field (bool) from true to false, I get the following exception:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

I'm the only one that's connected to the database, and after reading about concurrency conflicts, I can assure no other changes have been made to the record.

My code:

    public async Task<bool> InvalidateSession(Guid token)
    {
        var session = await _dbContext.SessionDetails.FirstOrDefaultAsync(sd => sd.Token.Equals(token))
            .ConfigureAwait(false);

        if (session == null)
            return false;

        session.IsValid = false;
        
        _dbContext.Update(session);
        await _dbContext.SaveChangesAsync().ConfigureAwait(false);

        return true;
    }

Hint: while debugging I noticed that although the only field I'm changing is 'IsValid = false', for some strange reason, the Token field is modified too (though it's value is the same):

As you can see in this screenshot: enter image description here

1 Answers

I can't repro this behavior. See if you can modify this to exhibit the behavior you are observing.

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace EfCore3Test
{

    [Table("SessionDetails")]
    public class SessionDetailsModel
    {
        public long Id { get; set; }
        public DateTime CreateDate { get; set; }
        public Guid Token { get; set; }
        public bool IsValid { get; set; }
    }
    public class Db : DbContext
    {
           
        public Db(): base()
        {

        }

        private static readonly ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
        {
            builder.AddFilter((category, level) =>
               category == DbLoggerCategory.Database.Command.Name
               && level == LogLevel.Information).AddConsole();
        });


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var constr = "Server = localhost; database = efcore3test; integrated security = true";

            optionsBuilder.UseLoggerFactory(loggerFactory)
                          .UseSqlServer(constr, o => o.UseRelationalNulls());

            //optionsBuilder.UseLazyLoadingProxies();
            base.OnConfiguring(optionsBuilder);
        }

        public DbSet<SessionDetailsModel> SessionDetails { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SessionDetailsModel>()
              .HasKey(x => new { x.Id, x.CreateDate });

            modelBuilder.Entity<SessionDetailsModel>().Property(e => e.Id).UseIdentityColumn();

            modelBuilder.Entity<SessionDetailsModel>().HasIndex(e => e.Token).IsUnique(true);

            base.OnModelCreating(modelBuilder);            
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

            long id;
            DateTime created = DateTime.Now;
            Guid token = Guid.NewGuid();
            
            using (var db = new Db())
            {

                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                var s = new SessionDetailsModel();
                s.CreateDate = created;
                s.Token = token;
                s.IsValid = true;
                db.SessionDetails.Add(s);
                db.SaveChanges();
                id = s.Id;
            }
            using (var db = new Db())
            {
                var sd = db.SessionDetails.Where(d => d.Token == token).Single();
                sd.IsValid = false;
                db.SaveChanges();
            }
        }
    }

}
Related