locked
DB First set primary key on model RRS feed

  • Question

  • User-268327207 posted

    I use DB first, have table name MsItem with ItemID as PK (I set it from sql server management studio).

    When i generate model from DB, ItemID is not recognized as PK (I guess) so there's field to input ItemID on my MsItem view.

    I used stored procedure to generate ItemID and insert data to MsItem table and it's success to insert data (The value from input field for ItemID is ignored because it's use ItemID generate by stored proc).

    I remove input field for ItemID on view and run my apps, when I insert data, i get EntityValidationErrors.

    I guess ItemID is required because it's string type and not recognized as PK so I think to start over create controller with adding [Key] atribute to ItemID on MsItem model.

    But still get input field for ItemID at MsItem view (And still get EntityValidationErrors if I remove that input field).

    Please some advice or solution, thank you.

    Wednesday, July 15, 2015 3:34 AM

Answers

  • User-84896714 posted

    Hi Ndra13,

    If the PK is string then it must be set from UI right (There must be a text field / hidden field) ?

    You could set the value ItemID in control or other place, but you have to make sure ItemID is not null before insert.

    public ActionResult Create(MsItem msItem)
    {
        if (ModelState.IsValid)
        {
            msItem.ItemID = "DefaultValue";
            db.MsItems.Add(msItem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    
        return View(msItem);
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2015 11:09 PM

All replies

  • User2024324573 posted

    I guess ItemID is required because it's string type

    Is it string type in your DB table? If it is true, then you cannot use auto insert and you need to provide value from the code. As it is NOT NULL and you are not adding to the record, the error is due to this. Check the table definition, update your EDMX and try again.

    Thanks,

    Wednesday, July 15, 2015 3:44 AM
  • User-84896714 posted

    Hi Ndra13,

    Thank you for your post.

    Ndra13

    ItemID is not recognized as PK (I guess)

     It recognized as PK, but not shown in entity class. If you see the edmx model, you could fine ItemID with a property Entity Key equals True.

    Ndra13

    when I insert data, i get EntityValidationErrors.

     When you execute insert method, entity framework will execute validate for you. ItemID is required. I suggest you use hidden field store a default value(for example: AAA) for ItemID. So it could pass validate and no need to input from user.

    Best Regards,
    Wang Li

    Thursday, July 16, 2015 1:32 AM
  • User-268327207 posted

    Thanks for your answer Mostafa and Wang Li.

    If the PK is string then it must be set from UI right (There must be a text field / hidden field) ?

    Is there no other way ?

    Best Regards,
    Hendra

    Monday, July 20, 2015 11:31 AM
  • User-84896714 posted

    Hi Ndra13,

    If the PK is string then it must be set from UI right (There must be a text field / hidden field) ?

    You could set the value ItemID in control or other place, but you have to make sure ItemID is not null before insert.

    public ActionResult Create(MsItem msItem)
    {
        if (ModelState.IsValid)
        {
            msItem.ItemID = "DefaultValue";
            db.MsItems.Add(msItem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    
        return View(msItem);
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2015 11:09 PM
  • User-268327207 posted

    Thank you Wang Li, I think that's better.

    Thursday, July 23, 2015 9:54 PM