locked
Create record and save in the database with incremented ID RRS feed

  • Question

  • User1670624291 posted

    Hi,

    I am currently performing a registration test, I have the very simple code generated by VS my doubt is that when creating a registration once my id is incremental I have the following error:

    public ActionResult Index()
            {
                return View(db.FichasTecnicasPDF.ToList());
            }
    
    
            // GET: FichasTecnicasPDFs/Create
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: FichasTecnicasPDFs/Create
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "ID,Nome,FilePatch")] FichasTecnicasPDF fichasTecnicasPDF)
            {
                if (ModelState.IsValid)
                {              
                    db.FichasTecnicasPDF.Add(fichasTecnicasPDF);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(fichasTecnicasPDF);
            }

    public partial class FichasTecnicasPDF
        {
            public int ID { get; set; }
            public string Nome { get; set; }
            public string FilePatch { get; set; }
        }

    How can I fix it so that my id is automatically incremented?
    Thanks,

    Monday, May 17, 2021 4:15 PM

Answers

  • User753101303 posted

    This message means that EF tries to insert a value for the ID column ie it uses an INSERT INTO MyTable(ID,Name) VALUES (x,'Some Value') when you should have INSERT INTO MyTable(Name) VALUES('Some value')

    For now I would double check the value for ID:
    - if not 0, make sure ID is left to ts default 0 value (never tried but maybe providing a vlaue takes precédence over being a server side generated value)
    - if 0, you perhaps misconfigured your entity so that ID is not configuired any more as a db side generated value cuasing EF to include its value in the INSERT statement

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2021 8:39 AM

All replies

  • User475983607 posted

    The error is very clear.  The table column is configured as an Identity (auto increment) column but the code tries to assign an Id.  Maybe you intend to update the record?

    db.FichasTecnicasPDF.Update(fichasTecnicasPDF);

    https://docs.microsoft.com/en-us/ef/core/saving/disconnected-entities

    Monday, May 17, 2021 5:35 PM
  • User-1545767719 posted

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "ID,Nome,FilePatch")] FichasTecnicasPDF fichasTecnicasPDF)
            {

    Try removing ID form white list of BindAttribute as follows:

    public ActionResult Create([Bind(Include = "Nome,FilePatch")] FichasTecnicasPDF fichasTecnicasPDF)

    Or remove input for ID in the View.

    Tuesday, May 18, 2021 5:57 AM
  • User1670624291 posted

    Hi,


    I tried with update but says it does not contain the definition for update.
    In relation to bind I take the ID, and give the same to the save.changes

    thanks,

    Tuesday, May 18, 2021 7:45 AM
  • User753101303 posted

    Hi,

    UInclear. You trried the suggestion about uisng

    public ActionResult Create([Bind(Include = "ID,Nome,FilePatch")] Fichas

    Is the ID value 0 ? IT is generated on the db side andso it should ne bot be part of the iNSERT statement. For now it seems something is wrong and that EF tries to insert a vlzue into the ID Column (maybe because it is currently not 0 or because something else is misconfigured).

    Tuesday, May 18, 2021 8:10 AM
  • User1670624291 posted

    Hi

    My table is empty because I want to insert values and that's why I set the auto increment 1-1.

    So I don't understand the error, because when inserting the other values, my db should generate the incremented id, right?

    Tuesday, May 18, 2021 8:19 AM
  • User753101303 posted

    This message means that EF tries to insert a value for the ID column ie it uses an INSERT INTO MyTable(ID,Name) VALUES (x,'Some Value') when you should have INSERT INTO MyTable(Name) VALUES('Some value')

    For now I would double check the value for ID:
    - if not 0, make sure ID is left to ts default 0 value (never tried but maybe providing a vlaue takes precédence over being a server side generated value)
    - if 0, you perhaps misconfigured your entity so that ID is not configuired any more as a db side generated value cuasing EF to include its value in the INSERT statement

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2021 8:39 AM
  • User1670624291 posted

    done

    thanks for helping me to think the right way, I updated my model and it worked, possibly at the time I had not yet defined the increment ... I'm going to sleep -_-

    Tuesday, May 18, 2021 9:16 AM