表单标签通是什么给后面的代码

问题描述 投票:0回答:1

在我工作的一个应用程序,允许用户添加注释。然而,当注释试图保存到数据库中,我得到一个错误。这些错误说...

SQLEXCEPTION:无法插入用于标识列显式值表“备注”当IDENTITY_INSERT设置为OFF。

DbUpdateException:更新条目中出现了错误。详情请参阅内部异常。

该数据库应该自动生成主键,但由于某种原因,注释模型分配Comment.ID(这是主键),目前该项目ID(即Submission.ID)用户评论。需要采取什么是应用程序应该允许DB分配的主键/ 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
    }
}
}
.net-core primary-key
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.