Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlException: Cannot insert explicit value for identity column in table 'Tasks' when IDENTITY_INSERT is set to OFF

I am creating this TaskManager app, and I've set up the model class, dbcontext and form.

When ever I press the submit button 2 times on the page itself, I get the following error:

Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'

Inner exception :

SqlException: Cannot insert explicit value for identity column in table 'Tasks' when IDENTITY_INSERT is set to OFF.

@page "/task/createtask"

@inject AppDbContext _context;

<h3>Create a Task</h3>

<EditForm Model="@taskModel" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <div class="form-group">
        <label for="TitleTb">Enter the title:</label>
        <InputText id="TitleTb" class="form-control" @bind-Value="taskModel.Title"></InputText>
        <label for="DescTb">Enter the description:</label>
        <InputText id="DescTb" class="form-control" @bind-Value="taskModel.Description"></InputText>
        <label for="SelectTb"> <span class="text-warning">optional</span> Enter the priority level:</label>
        <InputSelect id="SelectTb" class="form-control" @bind-Value="taskModel.ImportanceType">
            <option value="">Select priority...</option>
            <option value="1" class="text-danger">Very important</option>
            <option value="2" class="text-warning">Can wait</option>
            <option value="3" class="text-primary">Not that important</option>
        </InputSelect>
        <br/>
        <button type="submit" class="btn btn-primary">Submit!</button>
    </div>
</EditForm>

@code {
    private TaskModel taskModel = new TaskModel();

    private void HandleValidSubmit()
    {
        taskModel.IsCompleted = false;
        taskModel.DateCreated = DateTime.Today;

        _context.Tasks.Add(taskModel);
        _context.SaveChanges();
    }
}

Model and Context:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace TaskManagerApp.Models
{
    public class TaskModel
    {
        [Key]
        [Required]
        public int Id { get; set; }
        [Required]
        [MaxLength(20)]
        [MinLength(3)]
        public string Title { get; set; }
        [MaxLength(150)]
        [MinLength(5)]
        public string Description { get; set; }
        public DateTime DateCreated { get; set; }
        [Required]
        public bool IsCompleted { get; set; }
        public string ImportanceType { get; set; }
    }
}

DbContext:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TaskManagerApp.Models;

namespace TaskManagerApp.Data
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }

        public DbSet<TaskModel> Tasks { get; set; }
    }
}

Also I am using 2 dbcontexts, one from the Microsoft authentication system identity and another one that I created called AppDbContext.

Is there any way to solve it? If yes I would really appreciate your help!

Thank you!

like image 252
Isaac Newton Avatar asked Oct 24 '25 01:10

Isaac Newton


1 Answers

The first time you press submit the taskModel gets assigned a Id by the database.

The second time you press the button you are trying to add a new record with the same Id as the previous.

private void HandleValidSubmit()
{
    taskModel.IsCompleted = false;
    taskModel.DateCreated = DateTime.Today;
    _context.Tasks.Add(taskModel);
    _context.SaveChanges();

    this.taskModel = new TaskModel(); // <== Reset the model
}
like image 63
Brian Parker Avatar answered Oct 26 '25 15:10

Brian Parker