title: Why do I have an error: The database operation was expected to affect 1 row(s), but actually affected 0 row(s)?
body: <p>I have an error:
  Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: '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.</p>
  <p>when I am trying to add a comment to a post in database using Entity Framework and save changes. I don't know why. What am I doing wrong?</p>
  <p>Here I have the error:</p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class ValuesController : ControllerBase
  {
      private readonly ApplicationDbContext _applicationDbContext;
      public ValuesController(ApplicationDbContext applicationDbContext)
      {
          _applicationDbContext = applicationDbContext;
      }
  
      [HttpGet]
      public IActionResult Get()
      {
          var post = _applicationDbContext.Posts.First();
          post.AddComment(Guid.NewGuid(), &quot;aaa&quot;, &quot;bbb&quot;);
          _applicationDbContext.SaveChanges(); // here error
  
          return Ok();
      }
  }
  </code></pre>
  <p>Entity Framework configuration - probably something is wrong here?:</p>
  <pre><code>public class PostConfiguration : IEntityTypeConfiguration&lt;Post&gt;
  {
      public void Configure(EntityTypeBuilder&lt;Post&gt; builder)
      {
          builder.ToTable(&quot;Posts&quot;);
          builder.HasKey(x =&gt; x.PostId);
  
          builder.HasMany(x =&gt; x.Comments).WithOne().HasForeignKey(&quot;PostId&quot;);
      }
  }
  
  public class CommentConfiguration : IEntityTypeConfiguration&lt;Comment&gt;
  {
      public void Configure(EntityTypeBuilder&lt;Comment&gt; builder)
      {
          builder.ToTable(&quot;Comments&quot;);
          builder.HasKey(x =&gt; x.CommentId);
  
          builder.Property(x =&gt; x.CommentStatus)
              .HasColumnName(&quot;CommentStatusId&quot;)
              .HasConversion(new EnumToNumberConverter&lt;CommentStatus, int&gt;());
      }
  }
  
  public class ApplicationDbContext : DbContext
  {
      public DbSet&lt;Post&gt; Posts { get; set; }
      public DbSet&lt;Comment&gt; Comments { get; set; }
  
  
      public ApplicationDbContext(
          DbContextOptions options) : base(options)
      {
      }
  
  
      public override async Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
      {
          var result = await base.SaveChangesAsync(cancellationToken);
  
          return result;
      }
  
      protected override void OnModelCreating(ModelBuilder builder)
      {
          builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
  
          base.OnModelCreating(builder);
      }
  }
  </code></pre>
  <p>Here is my Post class:</p>
  <pre><code>public class Post
  {
      public Guid PostId { get; private set; }
  
      public string Title { get; private set; }
  
      public string Content { get; private set; }               
      
      public List&lt;Comment&gt; Comments { get; private set; } = new List&lt;Comment&gt;();
  
  
      public Post(Guid postId, string title, string content)
      {
          PostId = postId;
          Title = title;
          Content = content;
      }
  
  
      public void AddComment(Guid commentId, string author, string content)
      {
          var comment = new Comment(commentId, author, content);
          Comments.Add(comment);
      }
  }
  </code></pre>
  <p>Here is my Comment class:</p>
  <pre><code>public class Comment
  {
      public Guid CommentId { get; private set; }
  
      public string Author { get; private set; }
  
      public string Content { get; private set; }
  
      public CommentStatus CommentStatus { get; private set; }
  
  
      public Comment(Guid commentId, string author, string content)
      {
          CommentId = commentId;
          Author = author;
          Content = content;
          CommentStatus = CommentStatus.New;
      }
  }   
  </code></pre>
  <p>Here is my CommentStatus class:</p>
  <pre><code>public enum CommentStatus
  {
      New = 1,
      Accepted = 2,
      Rejected = 3
  }
  </code></pre>
  <p>In Program.cs I have:</p>
  <pre><code>var connectionString = builder.Configuration.GetConnectionString(&quot;DefaultConnection&quot;);
  builder.Services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; options.UseSqlServer(connectionString));
  </code></pre>