Link to home
Start Free TrialLog in
Avatar of Babak Sekandari
Babak SekandariFlag for United States of America

asked on

How to make a .NET Core MVC model ID not be required in a database first app

I have a .NET Core MVC database-first app wherein the Sql Server table's Primary Key shows up in the app as a required field.

I added the [Key] attribute above it; but it is still a required field:

        public int Id { get; set; }

Open in new window

This should be automatically incremented when I save / insert to the database.

The context class has the Id like this:

modelBuilder.Entity<LeaveRequest>(entity =>
                entity.Property(e => e.Id)

Open in new window

Here's how it looks when it renders:

User generated image

How do I fix this?

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Babak Sekandari


Yes, I think you are correct on the configuration because I found what is circled in red below, which is probably in error:
User generated image
User generated image
I'm going to change the identity to Yes; and then scaffold again.
Let me know, please, if you see anything else there which could be amiss.
Clicking the submit button still brings the message saying that the Id field is required.

After making the changes mentioned above and re-scaffolding, the entity property now reads:
 entity.Property(e => e.Id).HasColumnName("ID");

Open in new window

That didn't work so I changed it by hand to:
                // entity.Property(e => e.Id).HasColumnName("ID");
                entity.HasKey(e => e.Id).HasName("ID");

Open in new window

I commented out the validation line as shown below and then it no longer required that field; but then I don't understand why none of the other fields which had validation code required being filled out.
 <div class="form-group">
                    <label asp-for="Id" class="control-label"></label>
                    <input asp-for="Id" class="form-control" />
                    @*<span asp-validation-for="Id" class="text-danger"></span>*@

Open in new window

I then removed all the code above.

I'm not sure if what I did was the right thing to do so I'm still researching.

I would suggest not to do it by hand in the beginning. Once you have good hands on experience with Scaffolding, you will learn what not to do. OOB Scaffolding is only good for POC / learning perspective.

After you scaffolded DB context, you have to Scaffold pages / views and everything again to get the desired results.
The scaffolded model does not match the DB table. For example, DateTime fields in the model come across as some sort of text/string field in the *Context.cs file.
The table was originally a SharePoint list that I downloaded as an Excel file and uploaded to Sql Server as a table.
Should I close this question and begin another question to address that; or should I continue with the database table details further in this question?
For example, DateTime fields in the model come across as some sort of text/string field in the *Context.cs file.

Opening a new question is totally up to you. But if you downloaded a SP List I am not sure if DB will respect data types. You will have to have a closer look at the DB. I think your DB itself is messed up.
I set the data types manually on the table.

For date time values, I put, (datetime2(7), null)
Do you know of anything wrong with that?

I don't think that should create any issues. Could you try with a normal date as well? Does it come up as a string in the model?
I'm going to say that it is indeed a database issue.
I'm going to close this question because it will take a while to experiment with the database and see what the issue is.
For now, I think you got me on the right track to investigate further.
Perhaps when I've delved deeper and have collected more details, I'll open another question on MVC database first scaffolding.
No problem. Glad I could help.