I have Table student created in sql server 2005 by the class Student as following
- namespace UniversityData.Models
- {
- [Table("Student")]
- public partial class Student
- {
- public Student()
- {
- this.Classes = new HashSet
(); - }
- [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
- public int StudentID { get; set; }
- [Required]
- [StringLength(50)]
- public String StudentName { get; set; }
- [ForeignKey("Section")]
- public int SecID { get; set; }
- public ICollection
Classes { get; set; } - public virtual Section Section { get; set; }
- }
- }
I try to change StudentID to identity
so that i add DatabaseGeneratedOption.identity in place of DatabaseGeneratedOption.None
and i add in container class the following
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Entity
() - .Property(c => c.StudentID)
- .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
After that write in console add-migration aaa
it generated migration as following
- public partial class aaa : DbMigration
- {
- public override void Up()
- {
- DropForeignKey("dbo.Student_Class", "StudentID", "dbo.Student");
- DropPrimaryKey("dbo.Student");
- AlterColumn("dbo.Student", "StudentID", c => c.Int(nullable: false, identity: true));
- AddPrimaryKey("dbo.Student", "StudentID");
- AddForeignKey("dbo.Student_Class", "StudentID", "dbo.Student", "StudentID", cascadeDelete: true);
- }
- public override void Down()
- {
- DropForeignKey("dbo.Student_Class", "StudentID", "dbo.Student");
- DropPrimaryKey("dbo.Student");
- AlterColumn("dbo.Student", "StudentID", c => c.Int(nullable: false));
- AddPrimaryKey("dbo.Student", "StudentID");
- AddForeignKey("dbo.Student_Class", "StudentID", "dbo.Student", "StudentID", cascadeDelete: true);
- }
- }
- But finally not working what identity not working
- Please help me in that if possible
- I attached my topic with message
ahmed salahPosted Nov 17, 2016, 2:12 PM
Bikesh SrivastavaPosted Nov 17, 2016, 1:58 PM