1 Replies - 557 Views - Last Post: 04 January 2017 - 01:07 PM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

how to change department id primary key to identity and do migration t

Posted 10 November 2016 - 03:47 AM

Problem

I need to modify class Department to make Department ID identity then make affect by migrations to database sql server 2005 .

What I Try

I changed [DatabaseGenerated(DatabaseGeneratedOption.None)] from None to identity .

then from console write enable-migrations but it give me message

Migrations have already been enabled in project 'University Data'. To overwrite

the existing migrations configuration, use the -Force parameter.I also using

-Force but no any result or changed happened .

my class Department

[Table("Department")]
public partial class Department      { 
public Department()   
{    
}   
[DatabaseGenerated(DatabaseGeneratedOption.None)] 
[Key] 
public int DepartmentID { get; set; } 
public string DepartmentName { get; set; }  

  } 
 }


I have class department inside container Containerclass as following :

public partial class ContainerClass : DbContext     
 { 
public ContainerClass() : base("name=mycon")      
 {   
 }   
public virtual DbSet<Department> Departments { get; set; } 
 } 

Table department already created in database.

update

i write also in console

update-database but also nothing changed

update2

add-migration Department but also nothing changed column DepartmentID not changed to identity

update3

i add migration then write in up() and down()

AlterColumn("dbo.Department", "DepartmentID", c => c.Int(nullable: false, identity: true));

but it not change column to identity

Is This A Good Question/Topic? 0
  • +

Replies To: how to change department id primary key to identity and do migration t

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,565
  • Joined: 05-June 09

Re: how to change department id primary key to identity and do migration t

Posted 04 January 2017 - 01:07 PM

Have you checked your database to see if it's already an Identity column?

Assuming your partial class doesn't define anything such as public int Id { get; set; } it should be, by convention, your auto-incremented Identity primary key.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1