locked
How to create Auto generated column in Entity framework 6 RRS feed

  • Question

  • User-588203678 posted

    Hi,

    I want to create Auto generated column in Entity framework 6 using Code first approach

     public class Shopping
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            [Key]
    
            public int ProductId { get; set; }
            public string ProductCategory { get; set; }
            public string ProductName { get; set; }
            public string ProductQuantity { get; set; }
            public string Price { get; set; }
    
            //public virtual Items Item { get; set; }
        }

    this is not creating auto generated key on ProductId column.

    Thursday, December 22, 2016 1:20 AM

All replies

  • User-271186128 posted

    Hi guhananth1,

    As we all known, if the primary key column is int type, it will defined as Identity by default. We could also use the DatabaseGenerated attribute to set it.

    Based on your code, I create a sample on my side, it seems that everything works well. You could refer to the following screenshot:

    After migration, the migration file: as we can see the Identity attribute is true.

    Then the database as below:

    So, please try it again.

    If you want to set a start value for an Auto Incremented Column, you could refer to the following link:

    https://forums.asp.net/t/2062551.aspx?Entity+Framework+How+to+set+a+start+value+for+an+Auto+Incremented+Column+

    Best regards,
    Dillion

    Thursday, December 22, 2016 4:27 AM
  • User-588203678 posted

    Dillion

    Do i need to use this code for autoincremented id for primary key

    public partial class AddCustomer : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Customers",
                    c => new
                        {
                            CustomerID = c.Guid(nullable: false, identity: true),
                            CustomerNumber = c.Int(nullable: false, identity: true),
                            FirstName = c.String(),
                            MiddleName = c.String(),
                            LastName = c.String(),
                        })
                    .PrimaryKey(t => t.CustomerID);
                Sql("DBCC CHECKIDENT ('Employees', RESEED, 100);");
            }
            
            public override void Down()
            {
                DropTable("dbo.Customers");
            }
        }

    Thursday, December 22, 2016 4:43 AM
  • User-271186128 posted

    Hi guhananth1,

    Do i need to use this code for autoincremented id for primary key

    public partial class AddCustomer : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.Customers",
                    c => new
                        {
                            CustomerID = c.Guid(nullable: false, identity: true),
                            CustomerNumber = c.Int(nullable: false, identity: true),
                            FirstName = c.String(),
                            MiddleName = c.String(),
                            LastName = c.String(),
                        })
                    .PrimaryKey(t => t.CustomerID);
                Sql("DBCC CHECKIDENT ('Employees', RESEED, 100);");
            }
            
            public override void Down()
            {
                DropTable("dbo.Customers");
            }
        }

    No, above code is use to set the identity start value. By default the value is 1, you could use above code to change it to 100.

    Best regards,
    Dillion

    Thursday, December 22, 2016 10:16 AM