Click here to Skip to main content
15,353,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that I have users will be entering in new information to add a new record
I'm receiving an error.

Cannot insert the value NULL into column 'transID, table column does not allow nulls insert fails.
cshmtl:

<table id="myTable" class="table-striped">
         <tbody>
          <div class="row">
              <div class="col-md-4">
                  <form method="post">
                      @*<input type="hidden" value="tblAllTransactionsWithDetails.transId" name="Id" />*@
                      <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.GroupName" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.GroupName" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.GroupName" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.GroupNumber" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.GroupNumber" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.GroupNumber" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.FirstName" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.FirstName" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.FirstName" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.LastName" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.LastName" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.LastName" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.MiddleInitial" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.MiddleInitial" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.MiddleInitial" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.DateOfBirth" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.DateOfBirth" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.DateOfBirth" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <label asp-for="tblAllTransactionsWithDetails.DateofHire" class="control-label"></label>
                          <input asp-for="tblAllTransactionsWithDetails.DateofHire" class="form-control" />
                          <span asp-validation-for="tblAllTransactionsWithDetails.DateofHire" class="text-danger"></span>
                      </div>
                      <div class="form-group">
                          <input type="submit" value="Save" class="btn btn-primary" />
                      </div>
                  </form>


chstml PageModel:

public class AddNewRecordModel : PageModel
{
    private readonly ConnectionStringClass _db;

    public AddNewRecordModel(ConnectionStringClass db)
    {
        _db = db;
    }

    [BindProperty]
    public tblAllTransactionsWithDetails tblAllTransactionsWithDetails { get; set; }

    public IActionResult OnGet()
    {
        return Page();
    }

    // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
    public async Task<IActionResult> OnPostAsync()
    {

        //Displays Errors of Required fields if set in Model
        if (!ModelState.IsValid)
        {
            return Page();
        }


        //Remove transID?
        //ModelState.Remove("transID");

        _db.tblAllTransactionsWithDetails.Add(tblAllTransactionsWithDetails);

        await _db.SaveChangesAsync();

        return RedirectToPage("./Index");
    }

}


cs Model.

public class tblAllTransactionsWithDetails
  {

      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]

      public int transID { get; set; }

      public int bulkID { get; set; }

      public string Type { get; set; }

      public string SubType { get; set; }

      public string tUser { get; set; }

      public string status { get; set; }

      public int? numDetails { get; set; }

      public int? totalTrx { get; set; }


What I have tried:

I have tried setting the value manually, hard coding it.
ModelState.Remove the value
[DatabaseGenerated(DatabaseGeneratedOption.None)]
on the variable property.
The value on the Model when passed is transID = 0, but It's the primary key and it needs to be auto generated by the db.
Field is defined as transID(PK, int, not null)

Thanks for any help in advance.
Posted
Updated 31-Mar-22 21:42pm

1 solution

If the field needs to be generated by the database, then setting DatabaseGeneratedOption.None is clearly the wrong thing to do.

Removing the attribute won't update the database. And at least in MS SQL Server, it's not possible to change the IDENTITY property on a column that already exists, so you'll need to recreate your table to enable it.

Depending on the DBMS you're using, and the precise version of Entity Framework (presumably EF Core?), you may be able to add a default value using a database sequence object:
Sequences - EF Core | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900