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; }
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");
Here's how it looks when it renders:
How do I fix this?
entity.Property(e => e.Id).HasColumnName("ID");
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");
<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>
I then removed all the code above.For example, DateTime fields in the model come across as some sort of text/string field in the *Context.cs file.
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.