EF Requiring primary key when creating a new entry in sql table

Schanckopotamus

So we have a number of tables in our edmx, 13, that we created from an existing sql database. When we try to add a new entry to the tables through the UI we built, it fails on all but 4 of them.

Doing some digging we found out the the ModelState actually comes back as false and it is because we get an error that states "The [PrimaryKeyId] field is required". I don't understand what would be causing this issue when some of the tables are working just fine when we add new data.

enter image description here

In the edmx we looked at the properties of the primary keys of the tables and everything seems right

enter image description here

I feel like there could have been an issue when scaffolding the tables. I cannot be sure, I am still learning the intricacies of EF.

Can anyone point me in the right direction? Could this be something on the SQL side (personaly don't think it is as we already added data in these tables through sql scripts)? Could it be EF? Should I blow the edmx tables again and re-add them? I am feeling lost. Thanks for the help!

EDIT 1

So because we used the Entity Framework Scaffolding we have the basic methods. This is the create method with a few properties being manually set. Also the Primary Keys in the db are auto incrementing.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Entity entity)
{
    if (ModelState.IsValid)
    {
        _user = GetUser();
        entity.CreateDate = DateTime.Now;
        entity.LastUpdateDate = DateTime.Now;
        entity.LastUpdatedById = _user.Id;
        entity.CreatorId = _user.Id;

        db.Entities.Add(entity);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(entity);
}
Schanckopotamus

The problem has been solved!

Summary: After a day of problems and research for the solution it turned out to be something extreamly simple. Since we are creating entries to the sql database, we expose properties of a model to the view that we expect to be used by the user.

We were getting a single error causing the ModelState.IsValid to be false. The error was always the same, "[PrimaryKeyId] field is required".

Solution: In our views (.cshtml) we were exposing the primary key property but marking it as hidden. Apparently when you do this, the validation/checking of the model that EF does must check saying, to the effect, if the property is exposed in the view mark it as required and since it is never updated due to it being hidden it gets the default int value on 0, which is not valid as an identifier.

This is what the code was

<div class="bg-white content-inner">
@Html.HiddenFor(model => model.PrimaryKeyId)//BAD DO NOT DO THIS
    <div class="row-fluid">
        <div class="span4">
            @Html.LabelFor(model => model.GroupName)
        </div>
        <div class="span4 offset2">
            @Html.TextBoxFor(model => model.GroupName)
            @Html.ValidationMessageFor(model => model.GroupName)
        </div>
    </div>

Now without the property within the model we get this:

NO ERRORS AND A VALID MODELSTATE! NO ERRORS

VALID MODEL STATE

Given that I am still learning about EF and MVC this turned out to be a tough learning experience, but definatly a mistake I won't make again. Hope this can help!

Thanks to all gave me advice and helped.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Oracle SQL - Add Primary Key to table

From Dev

How to use default primary key as a sequence value when creating table for oracle database columns

From Dev

What is the impact of creating a table in SQL Server with a unique index but no primary key?

From Dev

How can the primary key be specified when creating a new model instance in Django Rest Framework?

From Dev

Get the new record primary key ID from SQL insert query to insert same value into another table?

From Dev

Using DeleteAllOnSubmit when the table has no primary key

From Dev

INSERT INTO new table setting Primary Key

From Dev

Gaps between primary key id in sql table

From Dev

How to get list of database table primary key columns in EF Core

From Dev

Why doesn't the primary key field exist in a MySQL database when creating a table?

From Dev

Max by primary key in EF

From Dev

In Linq to Sql how can I get the Id of row when creating new entry

From Dev

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

From Dev

Insert a new column into the Foreign Key table when a particular column in the primary key table is Update (Using Triggers)

From Dev

create primary key on existing table without primary key in SQL Server

From Dev

SQL Server insert table data script with new Primary Key

From Dev

PhpMyAdmin SQL code not showing up when creating new database and table

From Dev

Duplicate entry for key 'PRIMARY

From Dev

Creating new columns from data in other columns based on primary key

From Dev

MYSQL Duplicate entry 1 for key 'PRIMARY' when importing to another table

From Dev

INSERT INTO new table setting Primary Key

From Dev

SQL Table: Primary key ID not auto incrementing

From Dev

Is using SERIAL fine for the primary key when creating a table of all the comments made by users?

From Dev

Creating a primary key in sqlalchemy when creating a table instance

From Dev

Creating a SQL database without defining primary key

From Dev

Adding a media field when creating a new entry

From Dev

Duplicate entry '1' for key 'PRIMARY' when updating the table

From Dev

SQL Statement Error in Syntax When Creating Primary Key

From Dev

duplicate entry for key primary errors while replicating a table

Related Related

  1. 1

    Oracle SQL - Add Primary Key to table

  2. 2

    How to use default primary key as a sequence value when creating table for oracle database columns

  3. 3

    What is the impact of creating a table in SQL Server with a unique index but no primary key?

  4. 4

    How can the primary key be specified when creating a new model instance in Django Rest Framework?

  5. 5

    Get the new record primary key ID from SQL insert query to insert same value into another table?

  6. 6

    Using DeleteAllOnSubmit when the table has no primary key

  7. 7

    INSERT INTO new table setting Primary Key

  8. 8

    Gaps between primary key id in sql table

  9. 9

    How to get list of database table primary key columns in EF Core

  10. 10

    Why doesn't the primary key field exist in a MySQL database when creating a table?

  11. 11

    Max by primary key in EF

  12. 12

    In Linq to Sql how can I get the Id of row when creating new entry

  13. 13

    Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

  14. 14

    Insert a new column into the Foreign Key table when a particular column in the primary key table is Update (Using Triggers)

  15. 15

    create primary key on existing table without primary key in SQL Server

  16. 16

    SQL Server insert table data script with new Primary Key

  17. 17

    PhpMyAdmin SQL code not showing up when creating new database and table

  18. 18

    Duplicate entry for key 'PRIMARY

  19. 19

    Creating new columns from data in other columns based on primary key

  20. 20

    MYSQL Duplicate entry 1 for key 'PRIMARY' when importing to another table

  21. 21

    INSERT INTO new table setting Primary Key

  22. 22

    SQL Table: Primary key ID not auto incrementing

  23. 23

    Is using SERIAL fine for the primary key when creating a table of all the comments made by users?

  24. 24

    Creating a primary key in sqlalchemy when creating a table instance

  25. 25

    Creating a SQL database without defining primary key

  26. 26

    Adding a media field when creating a new entry

  27. 27

    Duplicate entry '1' for key 'PRIMARY' when updating the table

  28. 28

    SQL Statement Error in Syntax When Creating Primary Key

  29. 29

    duplicate entry for key primary errors while replicating a table

HotTag

Archive