Identity auto increment not applied on column

asp.net-mvc asp.net-web-api c# entity-framework entity-framework-6

Question

After the DB Migration when the tables were created in the Database, the Identity Auto-increment was not applied to the 'Id' Column whereas the Primary Key constraint was applied to it.
Following is the Model class code.

public class Subscription
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string Id { get; set; }
    public string domainName { get; set; }
    public string articleID { get; set; }
    public string tld { get; set; }
    public string customerID { get; set; }
    public string customerName { get; set; }
    public string price { get; set; }
    public string nextBillDate { get; set; }

}

And following are the columns from the Table that was generated.

enter image description here

Can someone please help me with how to cleanly apply auto increment in 'Id' Column. I want it to have both the Primary key and the auto increment.

Edit: I changed the type of Id to int but still the auto-increment is not applied to it.Here's a screenshot.

enter image description here

1
2
2/20/2018 9:40:09 AM

Accepted Answer

It's type should be int to make it Identity:

public int Id { get; set; }
1
2/20/2018 10:25:08 AM

Popular Answer

OK, after you change your Id from string to int and the problem still occurs, I recommend that you should delete your database(if you don't care about data) and delete your Migration file from Visual Studio as well. It is important for you to delete your migration file from program because sometimes it doesn't recognized all changes. Or, you can add this line of code by hand in your migration file(maybe not recommended, but if nothing helps, this will help):

identity: true

So, it should look something like this:

CreateTable("dbo.Table",
            c => new
            {
                Id= c.Int(nullable: false, identity: true),
                ...
            })
            .PrimaryKey(t => t.Id)
            ...


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow