1

I need to know how to implement the relationship in the model without using Entity Framework in C#.

enter image description here

Please give me a solution using this ER diagram.

class Student
        {
            public int studentId { get; set; }
            public string studentName { get; set; }
            public DateTime DOB { get; set; }
            public string street { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string PIN { get; set; }
            public int courseId { get; set; }
            public string courseName { get; set; }
        }
    class Course
        {
            public int courseId { get; set; }
            public string courseName { get; set; }
        }

    class StudentHobby
        {
            public int studentId { get; set; }
            public string studentName { get; set; }
            public string hobby { get; set; }
        }
    class Lecturer
        {
            public int lecturerId { get; set; }
            public string lecturerName { get; set; }
            public int courseId { get; set; }
            public string courseName { get; set; }
        }
8
  • 2
    SHOW US what YOU have tried so far! SO is not a free "write my code for me" service - YOU need to make an effort first, show us what you have and where you're stuck - then we'll help ..... Commented Mar 9, 2019 at 7:14
  • I am entirely new to programming. I can code but the thing is I want to learn the best practice. Commented Mar 9, 2019 at 7:26
  • 1
    Well, then start by coding the four entities you show in the ER diagram as C# classes, and post them here - then we can go a step further and introduce the relationships Commented Mar 9, 2019 at 7:27
  • I dont know how to work with foreign key in model...Do i need to have separate properties or i can call the class...I have doubt on those Commented Mar 9, 2019 at 7:31
  • 1
    Why are you specifically excluding Entity Framework here? That would make things a lot easier..... Commented Mar 9, 2019 at 7:53

1 Answer 1

1

Basically, your entity classes should always contain

  • their own "useful" data - e.g. attributes of that entity, like name and other information of the lecturer himself

  • foreign key attributes to link to other entities, e.g. the CourseId, but nothing more

The basic principle of relational design is making sure you have your relationships in place, and you do eliminate all data duplication -> e.g. do not store the CourseName with the Lecturer class, since if you would, then if the course's name changes, you have to start updating that not only in the Course table, but also in all instances of the Lecturer and possibly Student rows that use that course. That's exactly what you're trying to avoid.

The idea is this: with the foreign key attribute CourseId stored on the Lecturer, if you load a lecturer, you can also take the FK attribute and then also load the corresponding Course and get all its details - without having to duplicate information across multiple tables / entities.

If you would consider using Entity Framework, you could very easily include those fk links as "navigation properties" on your classes:

public class Lecturer
{
     // Lecturer's own attributes
     public int lecturerId { get; set; }
     public string lecturerName { get; set; }

     // FK attribute
     public int courseId { get; set; }

     // FK navigation property
     public virtual Course Course { get; set; }
}

and then the "EF magic" would automagically do this "loading" in the background.

Now in your code, you could load a Lecturer, and by navigating on its Course navigation property, you can access all details of the Course as well - without duplicating any data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.