[英]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.我正在创建这个 TaskManager 应用程序,并且我已经设置了 model class、dbcontext 和表单。
When ever I press the submit button 2 times on the page itself, I get the following error:当我在页面本身上按提交按钮 2 次时,我收到以下错误:
Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries.
Microsoft.EntityFrameworkCore.DbUpdateException: '更新条目时出错。 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.
SqlException:当 IDENTITY_INSERT 设置为 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: Model 和上下文:
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
.我还使用了 2 个 dbcontext,一个来自 Microsoft 身份验证系统身份,另一个来自我创建的
AppDbContext
。
Is there any way to solve it?有什么办法可以解决吗? If yes I would really appreciate your help!
如果是,我将非常感谢您的帮助!
Thank you!谢谢!
The first time you press submit the taskModel gets assigned a Id by the database.第一次按下提交时,taskModel 会被数据库分配一个 Id。
The second time you press the button you are trying to add a new record with the same Id as the previous.第二次按下按钮时,您尝试添加与前一个 ID 相同的新记录。
private void HandleValidSubmit()
{
taskModel.IsCompleted = false;
taskModel.DateCreated = DateTime.Today;
_context.Tasks.Add(taskModel);
_context.SaveChanges();
this.taskModel = new TaskModel(); // <== Reset the model
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.