oneManArmin
oneManArmin

Reputation: 696

ASP.NET with Entity Framework: EF does not generate identity automatically

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:

PersonID marked as identity

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:

ModeslState is false

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).

Upvotes: 2

Views: 1654

Answers (2)

oneManArmin
oneManArmin

Reputation: 696

Thank you for all your suggestions in the comment section, but surprisingly, the problem had nothing to do with the DB or the Model: I recreated the whole DB, but still got the same error. Turns out, the problem was in the view responsible for prompting input from the user upon creating a new entry, to be more specific, this was the problematic part:

<div class="form-group">
    @Html.HiddenFor(model => model.PersonID, htmlAttributes: new { @class = "control-label col-md-2" })
</div>

SOLUTION: I just deleted the whole <div> with the @Html.HiddenFor html helper for the id and the db now accepts new entries. Simple as that.

What I think caused the problem is that the db seemed to expect ID to be provided by the user even though the helper was set to hidden and I suppose, this is why ModelState.IsValid was always false. I used @Html.HiddenFor in the Edit feature (I wrote edit before create to test the db with mock data) and it had no problems, so I thought by disallowing the user to interact with a piece of data, ASP will automatically know not to expect it from the user, but clearly, I was wrong.

Upvotes: 1

Rasitha Ariyarathna
Rasitha Ariyarathna

Reputation: 171

Try this code to declare primary key

[PersonID] int IDENTITY(1,1) PRIMARY KEY

Upvotes: 0

Related Questions