C# 用于sql server查询的oop asp.net类
在sql server中,我有学生和系表。 我想从学生表中获取学生记录;从Departments表中获取他们的部门数据(因此我加入了他们),并在asp.net页面中显示它们 在asp.net中,我创建了一个学生类类型,以便在asp.net codebehind的列表中使用。 该列表包含以下学生班级类型数据:C# 用于sql server查询的oop asp.net类,c#,sql,sql-server,class,types,C#,Sql,Sql Server,Class,Types,在sql server中,我有学生和系表。 我想从学生表中获取学生记录;从Departments表中获取他们的部门数据(因此我加入了他们),并在asp.net页面中显示它们 在asp.net中,我创建了一个学生类类型,以便在asp.net codebehind的列表中使用。 该列表包含以下学生班级类型数据: List<Student> lst = new List<Student>(); 我的问题;我是否必须为每个查询创建另一个新的自定义类? 如果我为我使用的每个查询创
List<Student> lst = new List<Student>();
我的问题;我是否必须为每个查询创建另一个新的自定义类?
如果我为我使用的每个查询创建一个自定义类,那么解决方案中的类太多了。
我想我做错了。。
我该怎么办?
感谢您的帮助。您没有创建“学生和系”类
您在这里所做的是尝试“展平”域模型,并使用一个类来表示更好地定义为关系的东西
您可以创建域模型。并把它们联系起来
public class Student
{
public string LastName
/* the above is an example 'scalar' property on the Student. you'll have others like FirstName, StudentIdenficationNumber, etc, etc. */
/* below is the 'relationshiop' property, use one of the two below but not both */
public ICollection<Department> Departments;
/* or */
public Department ParentDepartment;
}
public class Department
{
public string DepartmentName
public ICollection<Student> Students;
}
差不多吧
现在,我通常会离开别人
我不编写JOIN SQL语句。
我编写两个单独的SQL语句(在一个存储过程中)来获取数据
dbo.uspStudentsGetAllWithParentDepartments
我想要这个
Select st.EmpKey, st.LastName, st.FirstName from dbo.Student st
Select dept.DepartmentKey, dept.DepartmentName from dbo.Department dept where exists (Select null from dbo.Student innerStud where innerStud.ParentDepartmentKey = dept.DepartmentKey )
现在,EntityFramework可以为您编写sql,但如果您以前从未见过它,这将是一项启动成本
我唯一的微软朋友不会提及的是,EF不支持NHibernate(另一种ORM工具)那样的.Merge()函数。这对我来说是个破坏者。但这是一个更深层次的讨论
定义您的域对象及其关系,然后询问有关“基于我当前的技能集,优化我的域模型的最佳方法是什么”(如果您对新方法持开放态度,则不使用技能集部分)
这里是我发布的另一个答案的链接…这是ado.net水合对象方式的序列化程序代码
我认为您确实需要仔细阅读entity framework,这几乎完全符合您的要求。是的,我是新来的。。你知道这种类型的例子吗?@sparky-PseudoNym01是对的。实体框架是未来的发展方向。首先,我要读这篇文章。谢谢大家。。
dbo.uspStudentsGetAllWithParentDepartments
Select st.EmpKey, st.LastName, st.FirstName from dbo.Student st
Select dept.DepartmentKey, dept.DepartmentName from dbo.Department dept where exists (Select null from dbo.Student innerStud where innerStud.ParentDepartmentKey = dept.DepartmentKey )