none
How to make identity in table starting by specific number from code first RRS feed

  • Question

  • I have class Student with column StudentID primary key  identity as following :

    namespace UniversityData.Models
    {
        [Table("Student")]
        public partial class Student
        {
            public Student()
            {
                this.Classes = new HashSet<Class>();
            }
           // [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
    
           [Key]
            public int StudentID { get; set; }
    
            [Required]
            [StringLength(50)]
            public String StudentName { get; set; }
            [ForeignKey("Section")]
            public int SecID { get; set; }
            public string Address { get; set; }
            public ICollection<Class> Classes { get; set; }
            public virtual Section Section { get; set; }
    
    
        }
    }

    Now i have table Student in database with column StudentID identity

    but my question

    How to make student id column start from number 1800 as identity from code first not manually by sql server

    are this possible in code first from visual studio 2015 .

    Thursday, November 17, 2016 3:51 PM

Answers

  • IF Possible can you tell me in which place can i write this

    suppose i have controller name configuration and i have function assign 

    and textbox textassign

    can you please help me how to assign number to textassign inside controller


    You don't write anything. You go to the DB itself like MS SQL Sever and you tell the DB through MS SQL Sever Management Studio what table, what column is to be an Identity Column in the table and what is the seeding number to start with as the seeding number. You do it that way or with a T-SQL script that does this and not the ORM nonsense.   
    Sunday, November 20, 2016 3:21 PM

All replies

  • I found solution for that but i cannot complete implementation

    i do as following

     public override void Up()
            {
                Sql("DBCC CHECKIDENT ('Student', RESEED, 1800)");
            }

    It is already working and start from 1801 but i need to make number dynamically 

    how i can do that

    dynamically meaning make user put number by his hand

    not force him to do that

    Thursday, November 17, 2016 7:16 PM
  • Hi eng-ahmed-salah,

    You could use DbContext.Database.ExecuteSqlCommand method to achieve it, like this:

    using (var db = new EFDemoContext())
                {
                    int start = 1800;
                    db.Database.ExecuteSqlCommand(string.Format("DBCC CHECKIDENT('Employee', RESEED, {0})",start));
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 18, 2016 3:34 AM
    Moderator
  • IF Possible can you tell me in which place can i write this

    suppose i have controller name configuration and i have function assign 

    and textbox textassign

    can you please help me how to assign number to textassign inside controller

    Friday, November 18, 2016 9:14 AM
  • IF Possible can you tell me in which place can i write this

    suppose i have controller name configuration and i have function assign 

    and textbox textassign

    can you please help me how to assign number to textassign inside controller


    You don't write anything. You go to the DB itself like MS SQL Sever and you tell the DB through MS SQL Sever Management Studio what table, what column is to be an Identity Column in the table and what is the seeding number to start with as the seeding number. You do it that way or with a T-SQL script that does this and not the ORM nonsense.   
    Sunday, November 20, 2016 3:21 PM