基于指定ER图,在C#中不使用Entity Framework创建并实现模型关系
Got it, let's break this down step by step—no Entity Framework required, just plain C# classes and proper relationship modeling based on the ER structure you shared.
1. First, Clean Up Redundant Fields in Your Classes
Your initial classes have duplicated data (like courseName in both Student and Course) which violates database normalization principles. Let's refactor the models to remove redundancy and set up proper relationships:
// Core Course model public class Course { public int CourseId { get; set; } public string CourseName { get; set; } // Navigation properties: One course has many students public List<Student> EnrolledStudents { get; set; } = new List<Student>(); // Navigation properties: One course can be taught by many lecturers public List<Lecturer> CourseLecturers { get; set; } = new List<Lecturer>(); } // Student model with address details public 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; } // Foreign key: Links to the student's course public int CourseId { get; set; } // Navigation property: One student belongs to one course public Course EnrolledCourse { get; set; } // Navigation property: One student has many hobbies public List<StudentHobby> StudentHobbies { get; set; } = new List<StudentHobby>(); } // Intermediate model for Student-Hobby many-to-many relationship public class StudentHobby { // Composite primary key (StudentId + Hobby) public int StudentId { get; set; } public string Hobby { get; set; } // Navigation property: Links back to the student public Student Student { get; set; } } // Lecturer model public class Lecturer { public int LecturerId { get; set; } public string LecturerName { get; set; } // Foreign key: Links to the course the lecturer teaches public int CourseId { get; set; } // Navigation property: One lecturer teaches one course public Course AssignedCourse { get; set; } }
2. Explaining the Model Relationships
Here's how each pair of models relates, aligned with standard ER diagram logic:
One-to-Many (1:N) Relationships
- Course ↔ Student: A single course can have multiple enrolled students (
Course.EnrolledStudentsis a list), while each student only belongs to one course (Student.EnrolledCourseis a single object, paired with theCourseIdforeign key). - Course ↔ Lecturer: A single course can be taught by multiple lecturers (
Course.CourseLecturersis a list), while each lecturer teaches one course (Lecturer.AssignedCourseis a single object, paired with theCourseIdforeign key). If your ER diagram specifies a lecturer can teach multiple courses, just swap the relationship: add aList<Course>toLecturerand move the foreign key to theCourseclass.
Many-to-Many (N:N) Relationship
- Student ↔ Hobby: A student can have multiple hobbies, and a hobby can be shared by multiple students. We use the
StudentHobbyintermediate class to handle this—no need for a separateHobbymodel unless your ER diagram defines one (if it does, add aHobbyclass with an ID and linkStudentHobbyto it via a foreign key).
3. Example Usage (No EF Needed)
Since we're not using Entity Framework, you'll manually manage object references and relationships. Here's how to instantiate and use the models:
// Create a course var introToCs = new Course { CourseId = 1, CourseName = "Introduction to C#" }; // Create a student and link to the course var janeDoe = new Student { StudentId = 1, StudentName = "Jane Doe", Dob = new DateTime(2002, 3, 10), Street = "456 Oak Ave", City = "Chicago", State = "IL", Pin = "60601", CourseId = introToCs.CourseId, EnrolledCourse = introToCs }; // Add hobbies to the student janeDoe.StudentHobbies.Add(new StudentHobby { StudentId = janeDoe.StudentId, Hobby = "Painting" }); janeDoe.StudentHobbies.Add(new StudentHobby { StudentId = janeDoe.StudentId, Hobby = "Gaming" }); // Create a lecturer and link to the course var profSmith = new Lecturer { LecturerId = 1, LecturerName = "Prof. Smith", CourseId = introToCs.CourseId, AssignedCourse = introToCs }; // Add student and lecturer to the course's navigation properties introToCs.EnrolledStudents.Add(janeDoe); introToCs.CourseLecturers.Add(profSmith); // Access related data Console.WriteLine($"{janeDoe.StudentName} is enrolled in {janeDoe.EnrolledCourse.CourseName}"); Console.WriteLine($"{profSmith.LecturerName} teaches {profSmith.AssignedCourse.CourseName}"); Console.WriteLine($"{janeDoe.StudentName}'s hobbies:"); foreach (var hobby in janeDoe.StudentHobbies) { Console.WriteLine($"- {hobby.Hobby}"); }
Key Takeaways
- No EF Dependency: This is all plain C#—you're just using object references to model relationships, no ORM required.
- Avoid Data Duplication: By using navigation properties instead of repeating fields (like
courseNameinStudent), you keep your models consistent and prevent data inconsistencies. - Flexible Adjustments: Tweak the navigation properties and foreign keys to match your exact ER diagram (e.g., make lecturers teach multiple courses if that's your use case).
内容的提问来源于stack exchange,提问作者Neroshan Vimaleswaran
相关产品推荐
相关产品推荐

