.net-core - 表单标签传递给代码背后的什么
问题描述
在我正在开发的应用程序中,允许用户添加评论。但是,当评论尝试保存到数据库时出现错误。错误说...
SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“注释”中插入标识列的显式值。
DbUpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常。
数据库应该自动生成主键,但由于某种原因,评论模型将(主键)分配给用户正在评论Comment.ID
的当前项目 ID(即)。Submission.ID
需要发生的是应用程序应该允许数据库分配主键/ Comment.ID
。
有人能告诉我为什么应用程序设置主键吗?
看法
<form method="post" class="col-12">
<input type="text" class="form-control" name="Body" placeholder="Add Comment" />
<input type="submit" class="btn btn-primary btn-sm my-2 float-right" />
</form>
控制器
public async Task<IActionResult> OnPostAsync(int id)
{
if (!ModelState.IsValid)
return Page();
// Adding values to fields automatically. These fields are not on the form for users to see and update.
Comment.SubmissionID = Submission.ID;
Comment.CreatedBy = "Andre";
Comment.CreatedAt = DateTime.Now;
_SubmissionContext.Comments.Add(Comment);
await _SubmissionContext.SaveChangesAsync();
return RedirectToPage("ProjectDetails", new { ID = id });
}
模型
public class Comment
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int SubmissionID { get; set; }
public Submission Submission { get; set; }
[Column(TypeName = "text")]
public string Body { get; set; }
public string CreatedBy { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g}")]
public DateTime CreatedAt { get; set; }
}
迁移类
namespace IST_Submission_Form.Migrations
{
[DbContext(typeof(SubmissionContext))]
partial class SubmissionContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.3-rtm-32065")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("IST_Submission_Form.Models.Comment", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Body")
.HasColumnType("text");
b.Property<DateTime>("CreatedAt");
b.Property<string>("CreatedBy");
b.Property<int>("SubmissionID");
b.HasKey("ID");
b.HasIndex("SubmissionID");
b.ToTable("Comment");
});
modelBuilder.Entity("IST_Submission_Form.Models.Submission", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("AssignedTo");
b.Property<DateTime>("Date");
b.Property<string>("DesiredCompletionDate")
.HasColumnName("Timeline");
b.Property<string>("Email");
b.Property<string>("Files");
b.Property<string>("FirstName");
b.Property<string>("Goal");
b.Property<string>("LastName");
b.Property<string>("Location");
b.Property<string>("LoginID");
b.Property<string>("ProjectDescription")
.HasColumnType("text");
b.Property<int>("Status");
b.Property<string>("Title");
b.HasKey("ID");
b.ToTable("Submissions");
});
modelBuilder.Entity("IST_Submission_Form.Models.Comment", b =>
{
b.HasOne("IST_Submission_Form.Models.Submission", "Submission")
.WithMany("Comments")
.HasForeignKey("SubmissionID")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
解决方案
所以我最终只是删除了数据库表和迁移并完成了重新创建它们的过程。我正在生产,所以这没什么大不了的。即使有我试图添加的评论,该应用程序现在也可以正常工作。
我认为问题是由于我在我的数据库上下文中添加了另一个表并且没有正确地进行我应该做的迁移。
作为记录,我的代码毕竟是正确的。迁移是问题所在。
推荐阅读
- google-chrome - 从特定进程捕获流量的 Fiddler 在 Chrome 中停止工作
- python - 如何使用pyspark数据框中的列标题动态删除列?
- swift - 有时方法会因致命错误而失败:UnsafeMutablePointer.initialize 重叠范围
- c - 增量问题
- r - 从 wordcore 预测中创建数据框
- javascript - 使用 axios post 请求下载图像
- r - R加快在光栅文件中添加常数
- azure-devops - 无法在 Azure DevOps yml 管道中解析 $(Release.ReleaseId)
- python - 如何为导入的模块配置日志记录?
- arrays - 如何将 json 字符串转换为 bash 中的单个字符数组?