Schanckopotamus
Schanckopotamus

Reputation: 791

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

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);
}

Upvotes: 2

Views: 1064

Answers (1)

Schanckopotamus
Schanckopotamus

Reputation: 791

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.

Upvotes: 4

Related Questions