Avatar of Babak Sekandari
Babak Sekandari
Flag 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:

[Key]
        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)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

Open in new window


Here's how it looks when it renders:


How do I fix this?

.NET ProgrammingDatabasesSQL

Avatar of undefined
Last Comment
Chinmay Patel

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chinmay Patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Babak Sekandari

ASKER
Yes, I think you are correct on the configuration because I found what is circled in red below, which is probably in error:


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.
thanks.
Babak Sekandari

ASKER
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




Babak Sekandari

ASKER
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>*@
</div>

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Chinmay Patel

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.
Babak Sekandari

ASKER
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?
Chinmay Patel

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Babak Sekandari

ASKER
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?

Chinmay Patel

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?
Babak Sekandari

ASKER
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.
thanks.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chinmay Patel

No problem. Glad I could help.