Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have two tables : Teacher and Lesson

Every teacher may have one or more lessons i create a new table and named it TeacherLesson :

SQL
CREATE TABLE [dbo].[TeacherLesson] (
[Id]        INT IDENTITY (1, 1) NOT NULL,
[teacherID] INT NULL,
[LessonID]   INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));


when user try to add a new teacher he/she can choose lessons for new teacher so i have a list of lessons that user can select them. that means i already have the lesson id but what about TeacherID
teacherID in teacher table is an identity specification (auto int) field so i don't know what is next id ! how i can insert new record in TeacherLesson Table with teacherID and LessonID
note that i use linq to SQL and WPF C#!

can i use foreign-key an make teacherID foreign-key and do something like this ? :

C#
Teachers.TeacherLesson.add(newTeacherLesson);



is it technically OK ?

thank you!

What I have tried:

SQL Table design Issue. one table that contains two table Keys
Posted
Updated 31-Jul-16 22:55pm

Have a look at using ExecuteScalar function to get the ID.
C#
Int32 newId = (Int32) myCommand.ExecuteScalar();


Check out
Retrieving Identity or Autonumber Values[^]
 
Share this answer
 
Hello ,
I think you can create 3 Tables for your requirement .
a. Lesson
b. Teacher
3. TeacherLesson (relation table between Teacher and Lesson )
Now , First insert data in Lesson table as you already have lessons . Now , when you are inserting data in Teacher at the same time insert record in TeacherLesson Table also means first insert data in Teacher table and then retreive the last inserted TeacherId from that table and then insert Lessons one by one for the TeacherId and the structure of TeacherLesson should be same as your definition means

CREATE TABLE [dbo].[TeacherLesson] (
[Id]        INT IDENTITY (1, 1) NOT NULL,
[teacherID] INT NULL,
[LessonID]   INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));


and for get last-inserted identity value you may use
SQL
@@IDENTITY


For more details Reference : Here

Hope it helps you .

Thanks
 
Share this answer
 
thank you very much for your answer

i use LINQ and WPF-C# not T-SQl !

the solution that i already use works but i want to know if it is technically OK and best for LINQ C# ?

C#
public static void InsertOrUpdateTeacher( string Name,string Family
,string Licence ,  byte[] Bytes,List<lesson> Lessons)
        {
 
            DataClassesDataContext dc = new DataClassesDataContext();
                Table<teacher> TeacherTable = dc.GetTable<teacher>();
                Teacher teacher = new Teacher();
                teacher.Name = Name;
                teacher.Family = Family;
                teacher.Licence = Licence;
                teacher.Image = Bytes;
                foreach (Lesson item in Lessons)
                {
                    TeacherLesson tl = new TeacherLesson();
                    tl.LessonID = item.Id;
                    Teacher.TeacherLessons.Add(tl);
                }
                TeacherTable.InsertOnSubmit(Teacher);
                TeacherTable.Context.SubmitChanges();
 

           
          
 
        }
</teacher></teacher></lesson>




and this is TeacherLesson table with Foreign-key

SQL
CREATE TABLE [dbo].[TeacherLesson] (
    [Id]        INT IDENTITY (1, 1) NOT NULL,
    [teacherID] INT NULL,
    [LessonID]   INT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_TeacherLesson_ToTable] FOREIGN KEY ([teacherID]) REFERENCES [dbo].[Teacher] ([Id])
);
 
Share this answer
 
v2
ISNULL(SCOPE_IDENTITY(), 0)


Best to write a transaction, to guarantee the integrity of the whole
 
Share this answer
 
v2

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



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