TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed salah
NA
530
148.8k
When change StudentID TO identity not work why
Nov 17 2016 9:52 AM
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
Reply
Answers (
2
)
Multiple File Upload in Gridview for each row
How to create macro to browse a file and alter column?