Tech
Forums
Jobs
Books
Events
Live
Interviews
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
When change StudentID TO identity not work why
ahmed salah
Nov 17 2016 9:52 AM
269
Reply
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 have already Table Student in database with no column identity
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
Attachment:
code1.rar
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
Multiple File Upload in Gridview for each row
How to create macro to browse a file and alter column?