Hello! Maybe someone faced a similar problem, but I did not find a question on the topic (although it’s bad) Google throws it on the github where this error was considered in the case of updating the record. In my case, it occurs when it is added. I ask a question before I understand the guide on MSDN (he, too, as I understood from the code about updating the record) pointed to by the action, since in any case, I will come here if nothing happens ...

... well, now more on the case: screen

Возникло исключение: CLR/Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException An exception of type 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: '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.' 

Orders.cs:

 using System; using System.Collections.Generic; namespace KupcheAspNetCore { public partial class Orders { public Orders() { Orderfiles = new HashSet<Orderfiles>(); Orderimages = new HashSet<Orderimages>(); } public int IdOrders { get; set; } public string Caption { get; set; } public string Text { get; set; } public string Geomap { get; set; } public decimal? Cost { get; set; } public int? Viewers { get; set; } public int UsersId { get; set; } public sbyte? ThereImages { get; set; } public sbyte? ThereFiles { get; set; } public sbyte? IsDeleted { get; set; } public DateTimeOffset AdditionTime { get; set; } public DateTimeOffset LastUpdate { get; set; } public Users Users { get; set; } public ICollection<Orderfiles> Orderfiles { get; set; } public ICollection<Orderimages> Orderimages { get; set; } } } 

Coutnry.cs:

 using System; using System.Collections.Generic; namespace KupcheAspNetCore { public partial class Country { public Country() { City = new HashSet<City>(); } public int IdCountry { get; set; } public string Name { get; set; } public string ShortName { get; set; } public DateTimeOffset AdditionTime { get; set; } public DateTimeOffset LastUpdate { get; set; } public ICollection<City> City { get; set; } } } 

ordersController.cs:

 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using KupcheAspNetCore.Models; namespace KupcheAspNetCore.Controllers { [Route("api/[controller]")] public class OrdersController : Controller { [HttpPost] public IActionResult PostOrders([FromBody]Orders order) { if(ModelState.IsValid) { Country neworder = new Country(); neworder.Name = "caption"; neworder.ShortName = "texttextextext"; // neworder.Cost = 6; // neworder.UsersId = 1; using(servicedbContext db = new servicedbContext()){ db.Country.Add(neworder); db.SaveChanges(); Console.WriteLine("Post response order: "+ neworder.Name.ToString()); return Ok(neworder); } } else { return BadRequest(ModelState); } } } } 

I tried to take a simpler table (seen on the screenshot). Is it also possible that this somehow concerns MySQL specifically? Thank you in advance!

  • I think it's worth logging queries to the database and finding out why rowsAffected = 0 - tcpack4
  • Can this be done using the logger or is it built into the EF methods? - Lenny Marks
  • google, there are ways for EF, a package for an external logger, for instance, you can enable the mysql configuration in the configs so that all queries are saved. - tcpack4
  • Thank you I looked at the requests, for some reason the generated context put a ban on the generation of Id (auto increment) decided by replacing the entity.Property(e => e.IdActivityTypes) .HasColumnName("idActivityTypes") .HasColumnType("int(11)") .ValueGeneratedNever(); On entity.Property(e => e.IdActivityTypes) .HasColumnName("idActivityTypes") .HasColumnType("int(11)") .ValueGeneratedOnAdd(); - Lenny Marks

1 answer 1

Thank you I looked at the requests, for some reason the generated context put a ban on the generation of Id (auto increment) was decided by replacing
entity.Property(e => e.IdActivityTypes) .HasColumnName("idActivityTypes") .HasColumnType("int(11)") .ValueGeneratedNever();
On
entity.Property(e => e.IdActivityTypes) .HasColumnName("idActivityTypes") .HasColumnType("int(11)") .ValueGeneratedOnAdd();