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

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
ahmedsa
Posts: 9
Joined: Mon 15 Aug 2016 10:17

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

Post by ahmedsa » Thu 10 Nov 2016 11:21

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

Code: Select all

[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 :

Code: Select all

public partial class ContainerClass : DbContext     
 { 
public ContainerClass() : base("name=mycon")      
 {   
 }   
public virtual DbSet<Department> Departments { get; set; } 
 } 
Table department already created in database.

update

1-
i write also in console

update-database but also nothing changed

2-

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

3-

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

Code: Select all

AlterColumn("dbo.Department", "DepartmentID", c => c.Int(nullable: false, identity: true));
but it not change column to identity

Pinturiccio
Devart Team
Posts: 2192
Joined: Wed 02 Nov 2011 09:44

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

Post by Pinturiccio » Fri 11 Nov 2016 12:42

Is your project code generated by a predefined template of Entity Developer or written by you manually?

ahmedsa
Posts: 9
Joined: Mon 15 Aug 2016 10:17

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

Post by ahmedsa » Fri 11 Nov 2016 14:36

it is written manually by code first

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Wed 16 Nov 2016 07:14

This question exceeds the goals of our support, the provider used is a standard one (System.Data.SqlClient). You should ask this question at MSDN forum.
JIC: if you wrote migration manually and ran it, check carefully the error message in Package Manager Console for tips to solve it.

Post Reply