I'm quite new to ASP and I'm creating a simple ASP.NET MVC App that stores personal info in a database. Using a DB First approach, I created the following db table:
CREATE TABLE [dbo].[Author] (
[PersonID] INT IDENTITY (1, 1) NOT NULL,
[MobileNum] NVARCHAR (50) NULL,
[Location] NVARCHAR (50) NULL,
[LinkedIn] NVARCHAR (50) NULL,
[FaceBook] NVARCHAR (50) NULL,
[Picture] IMAGE NULL,
[Name] NVARCHAR (50) NULL,
[Email] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([PersonID] ASC)
);
I use PersonID as a PrimaryKey
and I want EF to autogenerate this for me, but for some reason this does not happen.
I adjusted the identity options manually among the properties:
and I also attached the necessary attributes to my model (although I read somewhere that autogeneration is supposed to work without them as well):
public partial class Author
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PersonID { get; set; }
public string MobileNum { get; set; }
public string Location { get; set; }
public string LinkedIn { get; set; }
public string FaceBook { get; set; }
public byte[] Picture { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
Yet, the ModelState
of the db is always false during the creation of a new entry:
and taking a closer look at it in the debugger, the problem seems to be that EF expects PersonID to be assigned by the user.
I have the following View
belonging to the Create Action:
<div class="form-group">
@Html.HiddenFor(model => model.PersonID, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
Is there something else I'm supposed to do to enable the autogeneration of PersonIDs? What am I missing? Pointing me to a beginner level, DB First article is also much appreciated, since most articles I found did not make any mention of this problem whatsoever (e.g., here, here, or here).