Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have a problem with my database.It worked until today when I tried to build it and it said that my db relationship is not good"SQL71516: The referenced table '[dbo].[Courses]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted" and also "Error 159: EntityType 'DatabaseStudentsModel.Login' has no key defined. Define the key for this EntityType"In the second case,I already have the keys defined...These are my tables:
CREATE TABLE [dbo].[RegisterTeacher] (
    [SNTeacher]  INT            NOT NULL,
    [UserName]   NVARCHAR (30)  NOT NULL,
    [pwd]        INT            NOT NULL,
    [fullName]   NVARCHAR (MAX) NOT NULL,
    [courseID] INT NOT NULL,
    [education]  NVARCHAR (50)  NULL,
    PRIMARY KEY CLUSTERED ([SNTeacher]), 
    CONSTRAINT [FK_RegisterTeacher_ToTable] FOREIGN KEY ([courseID]) REFERENCES [Courses]([courseID])
);

CREATE TABLE [dbo].[Login] (
    [SNTeacher] INT           NULL,
    [UserName]  NVARCHAR (10) NOT NULL,
    [pwd]       INT           NOT NULL,
    PRIMARY KEY CLUSTERED ([pwd]),
    CONSTRAINT [FK_Login_ToTable] FOREIGN KEY ([SNTeacher]) REFERENCES [dbo].[RegisterTeacher] ([SNTeacher])
);

CREATE TABLE [dbo].[Courses] (
    [courseID]   INT            NOT NULL,
    [courseName] NVARCHAR (MAX) NOT NULL,
    [education]  NVARCHAR (50)  NOT NULL,
    [SNTeacher]  INT            NOT NULL,
    [ClassID]    NVARCHAR (18)  NULL,
    PRIMARY KEY CLUSTERED ([courseID] ASC),
    CONSTRAINT [FK_Courses_ToTable] FOREIGN KEY ([SNTeacher]) REFERENCES [dbo].[RegisterTeacher] ([SNTeacher])
);

CREATE TABLE [dbo].[ChildCourse] (
    [courseID] INT           NOT NULL,
    [UserName] NVARCHAR (10) NOT NULL,
    [sNr]      INT           NOT NULL,
    PRIMARY KEY CLUSTERED ([courseID] ASC), 
    CONSTRAINT [FK_ChildCourse_ToTable] FOREIGN KEY ([sNr]) REFERENCES [Courses]([SNTeacher])
);

What should I do in order to get rid of the error and what did I do wrong when it came about establishing the relations between the tables?Thank you in advance!

What I have tried:

I have tried to change the PK and FK for Courses,Login and RegisterTeacher,but the database won't allow me to do anything,even if I change sth,the table won't alter.
Posted
Updated 9-Apr-18 0:43am

1 solution

You have bidirectional referenced tables.

I suggest to remove the courseID column and the reference from the RegisterTeacher table (at least when a teacher can hold multiple courses).

If you really need to use bidirectional referenced tables you have to use an ALTER statement:
SQL
CREATE TABLE [dbo].[Courses] (
    [courseID]   INT            NOT NULL,
    [courseName] NVARCHAR (MAX) NOT NULL,
    [education]  NVARCHAR (50)  NOT NULL,
    [SNTeacher]  INT            NOT NULL,
    [ClassID]    NVARCHAR (18)  NULL,
    PRIMARY KEY CLUSTERED ([courseID] ASC)
);

ALTER TABLE [dbo].[Courses] ADD
    CONSTRAINT [FK_Courses_ToTable] FOREIGN KEY ([SNTeacher]) REFERENCES [dbo].[RegisterTeacher]);
);
 
Share this answer
 
Comments
Daniel Andrei Popescu 9-Apr-18 6:54am    
Hello,Thank you for your response.I managed to do it,but I have another problem with my database.I created the db in Visual Studio and I'm using DB first in my project,and I noticed that the error:Error 159: "EntityType 'DatabaseStudentsModel.Login' has no key defined" is persisting.I have deleted the table from the db,but when I want to create a new one it says that a table like the one I want to create already exists.How can I manage to get into the db and delete the table there or to make it work ?Best regards,
Jochen Arndt 9-Apr-18 7:13am    
Dropping (deleting) a table does not work when it is referenced. You have to delete the referenc(es) first. Because you are just starting, deleting the whole database and re-creating the tables might be simpler and ensures that it works.
Daniel Andrei Popescu 9-Apr-18 7:20am    
And if I'm using DB first,would this be a problem?
Jochen Arndt 9-Apr-18 7:28am    
I have not used that so far so that I can't really help.

But if it does how it reads (creates model codes from an existing database), you have to do that again after deleting the old created content because the database design has been changed.
Daniel Andrei Popescu 9-Apr-18 7:32am    
Ok,thank you for your response.If you will make your comment an answer,I will mark it as"Correct".Best wishes!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900