You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于指定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.EnrolledStudents is a list), while each student only belongs to one course (Student.EnrolledCourse is a single object, paired with the CourseId foreign key).
  • Course ↔ Lecturer: A single course can be taught by multiple lecturers (Course.CourseLecturers is a list), while each lecturer teaches one course (Lecturer.AssignedCourse is a single object, paired with the CourseId foreign key). If your ER diagram specifies a lecturer can teach multiple courses, just swap the relationship: add a List<Course> to Lecturer and move the foreign key to the Course class.

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 StudentHobby intermediate class to handle this—no need for a separate Hobby model unless your ER diagram defines one (if it does, add a Hobby class with an ID and link StudentHobby to 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 courseName in Student), 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:29:47