Reputation: 53
I am using DatabaseFirst approach in Entity Framework, I am getting Error :
The table does not have a primary key defined.
Now, when I am Scaffolding for Controller it is giving me
One or More Entities have Validation Error
Must be because of the EF error has its effect in here.
Earlier, when it was giving me the Primary Key error, I added column in table StudentAddress
& StudentCourse
and make it Primary KEY
just to get rid of the above Error. But added columns are not reflected back in my diagram(*.edmx), though in DB its reflecting back.
DB Script is:
create table [Standard](
StandardID int constraint pk_standard primary key,
StandardName varchar(255),
[Description] varchar(255)
);
create table Teacher(
TeacherID int constraint pk_teacher primary key,
TeacherName varchar(255),
StandardID int constraint fk_teacher foreign key references [Standard](StandardID) ,
TeacherType varchar(255)
);
create table Course(
CourseID int constraint pk_course primary key,
CourseName varchar(255),
Location varchar(255),
TeacherID int constraint fk_course foreign key references Teacher(TeacherID) ,
);
create table Student(
StudentID int constraint pk_student primary key,
StudentName varchar(255),
StandardID int constraint fk_student foreign key references [Standard](StandardID) ,
);
create table StudentAddress(
[Id] int constraint pk_SA primary key,
StudentID int constraint fk_studentaddr foreign key references Student(StudentID) not null,
Addr1 varchar(255),
Addr2 varchar(255),
City varchar (50),
[State] varchar (50)
);
create table StudentCourse(
[Index Number] int constraint pk_SC primary key,
StudentID int constraint fk1_studentcourse foreign key references Student(StudentID) not null,
CourseID int constraint fk2_studentcourse foreign key references Course(CourseID) not null,
);
PS : Creating WebApp using WebApi2 and EF(for data).
Upvotes: 3
Views: 2284
Reputation: 109281
I bet you didn't define primary keys for StudentAddress
and StudentCourse
in the original model. And now, by adding columns to it you change the character of the model significantly.
I think this is what you intended to have:
create table StudentAddress(
StudentID int constraint fk_studentaddr foreign key references Student(StudentID) not null,
Addr1 varchar(255),
Addr2 varchar(255),
City varchar (50),
[State] varchar (50),
constraint pk_SA primary key (StudentID) -- primary key
);
create table StudentCourse(
StudentID int constraint fk1_studentcourse foreign key references Student(StudentID) not null,
CourseID int constraint fk2_studentcourse foreign key references Course(CourseID) not null,
constraint pk_SC primary key (StudentID, CourseID) -- composite primary key
);
This makes Student-StudentAddress
a 1:1
association and Student-StudentCourse
a n:m
association. Thus, Student-StudentCourse
can be hidden in the class model.
So you should only add primary key constraints in stead of columns.
Here are the consequences of adding columns:
By adding another column as primary key to StudentAddress
, the association Student-StudentAddress
turns into a 1:n
association (because StudentAddress.StudentID
can now be duplicated).
With the additional Index Number
column, Student-StudentCourse
remains a n:m
association, but it can no longer be hidden in the class model, because that's only possible if the junction table only contains two foreign keys that both comprise a primary key.
I'd create a new edmx from the model above it it suits you. Refreshing the model is probably not going too work well with the modified primary keys.
This is what the edmx should look like after the modifications:
Upvotes: 1
Reputation: 90
You should update your model class.
In the Models folder, open the *.edmx file to display the model diagram, Right-click anywhere on the design surface, and select Update Model from Database.
For more information: http://www.asp.net/mvc/overview/getting-started/database-first-development/changing-the-database
Upvotes: 0