如何在C#中不使用实体框架创建模型?

如何在C#中不使用实体框架创建模型?,c#,orm,entity-relationship,C#,Orm,Entity Relationship,我需要知道如何在模型中实现关系,而不使用C#中的实体框架 请用这个ER图给我一个解决方案 class Student { public int studentId { get; set; } public string studentName { get; set; } public DateTime DOB { get; set; } public string street {

我需要知道如何在模型中实现关系,而不使用C#中的实体框架

请用这个ER图给我一个解决方案

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; }
        }

基本上,实体类应该始终包含

  • 他们自己的“有用”数据——例如该实体的属性,如讲师本人的姓名和其他信息

  • 用于链接到其他实体的外键属性,例如
    CourseId
    ,仅此而已

关系设计的基本原则是确保你有适当的关系,并消除所有数据重复->例如,使用
讲师
类存储
课程名称
,因为如果你愿意,那么如果课程名称更改,您不仅必须在
课程
表中,而且还必须在使用该课程的
讲师
的所有实例中以及可能的
学生
行中开始更新。这正是你想要避免的

其思想是:使用存储在
讲师
上的外键属性
CourseId
,如果加载讲师,还可以获取FK属性,然后加载相应的
课程
,并获取其所有详细信息,而无需跨多个表/实体复制信息

如果您考虑使用<强>实体框架< /强>,则可以< <强> >非常容易地< /强>将那些FK链接包含为类的“导航属性”:

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; }
}
然后“EF魔法”会自动在后台进行“加载”


现在,在您的代码中,您可以加载一个
讲师
,通过在其
课程
导航属性上导航,您也可以访问
课程
的所有详细信息,而无需复制任何数据。

向我们展示您迄今为止的尝试!所以,这不是一个免费的“为我编写代码”服务——你需要先付出努力,向我们展示你拥有的东西和你的困境——然后我们会提供帮助……我对编程一无所知。我可以编写代码,但问题是我想学习最佳实践。那么,首先将ER图中显示的四个实体编码为C#类,在这里发布它们-然后我们可以更进一步,介绍关系我不知道如何在模型中使用外键…我需要单独的属性还是可以调用类…我对这些有疑问为什么你在这里特别排除实体框架?那会让事情变得容易很多。。。。。