如何添加自动生成的ID继续从我最后的数据

本文关键字:继续 ID 数据 最后的 自动生成 何添加 添加 | 更新日期: 2023-09-27 18:16:20

我想自动生成从我以前的ID递增的ID。ID格式为A00001, A00002,... ..我不知道如何自动生成

控制器

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {
        if (ModelState.IsValid)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase assignmentFile = Request.Files[0];
                if (assignmentFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(assignmentFile.FileName);
                    assignment.FileLocation = Path.Combine(Server.MapPath("~/Content/File"), fileName);
                    assignmentFile.SaveAs(assignment.FileLocation);
                }

            }
            db.Assignments.Add(assignment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(assignment);
    }

  public partial class Assignment
   {
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }
    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation { get; set; }
    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}
<<h2> 视图/strong>
 <% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
 { %>
  <%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Assignment</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.SubmissionDate) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.SubmissionDate) %>
        <%: Html.ValidationMessageFor(model => model.SubmissionDate) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Status) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Status) %>
        <%: Html.ValidationMessageFor(model => model.Status) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Mark) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Mark) %>
        <%: Html.ValidationMessageFor(model => model.Mark) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Comments) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Comments) %>
        <%: Html.ValidationMessageFor(model => model.Comments) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FileLocation) %>
    </div>
    <div class="editor-field">
       <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

我想自动生成从我的前一个ID递增的ID。ID格式为A00001, A00002,... ..我不知道如何自动生成

如何添加自动生成的ID继续从我最后的数据

try this

        db.Assignments.Add(assignment);
        db.SaveChanges();
        Var AssignmentID = assignment.Id;

一开始我误解了你的问题。

您需要标识是首字母a的字符串吗?你可以使用数据库的identity属性来实现自动递增然后使用get来创建字符串,如果你需要的话?

下面的代码用于代码优先迁移。如果你使用数据库优先的方法,那么不需要[Key]和[NotMapped]注释。在这种情况下,只需创建Id属性为identity(1,1)。

public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }
    [Key]
    private int _id; // Auto incremented id
    [NotMapped]
    public string AssignmentID
    {
        get
        {
            return "A" + _id.ToString("D5");
        }
    }
    public DateTime? SubmissionDate { get; set; }
    public string Status { get; set; }
    public decimal? Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation { get; set; }
    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}

这样,您将拥有数据库自动增量的可靠性和在类定义中格式化特殊Id的灵活性。

编辑:完全重写了我的答案

试试这个,

con.Open();
            string sqlQuery = "SELECT TOP 1 kode_user from USERADM order by kode_user desc";
            SqlCommand cmd = new SqlCommand(sqlQuery, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string input = dr["kode_user"].ToString();
                string angka = input.Substring(input.Length - Math.Min(3, input.Length));
                int number = Convert.ToInt32(angka);
                number += 1;
                string str = number.ToString("D3");
                txtKodeUser.Text = "USR" + str;
            }
            con.Close();

代码将获取数据库中的最后一个id,然后仅从字符串中获取数字并将数字增加1。这是代码从string获取number的行:

string angka = input.Substring(input.Length - Math.Min(3, input.Length));