2 years ago

#75486

test-img

Probir Roy

EF Core 6.0 Insert Failure: The database operation was expected to affect 1 row(s), but actually affected 0 row(s)

I am using

  • SQL Server 2019 Express
  • Visual Studio 2022
  • EF Core 6.0
  • .Net Core 6.0

My database structure:

CREATE TABLE [temp].[REGISTRATION]
(
    [id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [companyname] [varchar](50) NOT NULL,
    [firstname] [varchar](50) NOT NULL,
    [middlename] [varchar](50) NULL,
    [lastname] [varchar](50) NOT NULL,
    [genderid] [int] NULL,
    [email] [varchar](50) NOT NULL,
    [phone] [varchar](15) NOT NULL,
    [whatsappno] [varchar](15) NULL,
    [username] [varchar](25) NOT NULL,
    [encypass] [varchar](100) NOT NULL
)

Model class:

    [Key]
    [Column("id")]
    public int Id { get; set; }
    [Required]
    [Column("companyname")]
    [StringLength(50)]
    [Unicode(false)]
    public string Companyname { get; set; }
    [Required]
    [Column("firstname")]
    [StringLength(50)]
    [Unicode(false)]
    public string Firstname { get; set; }
    [Column("middlename")]
    [StringLength(50)]
    [Unicode(false)]
    public string Middlename { get; set; }
    [Required]
    [Column("lastname")]
    [StringLength(50)]
    [Unicode(false)]
    public string Lastname { get; set; }
    [Column("genderid")]
    public int? Genderid { get; set; }
    [Required]
    [Column("email")]
    [StringLength(50)]
    [Unicode(false)]
    public string Email { get; set; }
    [Required]
    [Column("phone")]
    [StringLength(15)]
    [Unicode(false)]
    public string Phone { get; set; }
    [Column("whatsappno")]
    [StringLength(15)]
    [Unicode(false)]
    public string Whatsappno { get; set; }
    [Required]
    [Column("username")]
    [StringLength(25)]
    [Unicode(false)]
    public string Username { get; set; }
    [Required]
    [Column("encypass")]
    [StringLength(100)]
    [Unicode(false)]
    public string Encypass { get; set; }

API Post Code:

[HttpPost]
public async Task<ActionResult<Registration>> PostRegistration(Registration registration)
{
    _context.Registrations.Add(registration);
    await _context.SaveChangesAsync();

    return CreatedAtAction("GetRegistration", new { id = registration.Id }, registration);
}

Request Body:

{
    "id": 0,
    "companyname": "string",
    "firstname": "string",
    "middlename": "string",
    "lastname": "string",
    "genderid": 0,
    "email": "string",
    "phone": "string",
    "whatsappno": "string",
    "username": "string",
    "encypass": "string"
}

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. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions

After inserting one row manually in the SQL Server table with an INSERT INTO query, the code (HTTPPost) executes properly.

I also used:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

but it still throws the same error.

Please help.

c#

entity-framework-core

asp.net-core-webapi

.net-6.0

ef-core-6.0

0 Answers

Your Answer

Accepted video resources