我有以下模型,对应于下面列出的数据库表。
经理是员工。会计师也是雇员。
CODE
MyRepository.MyEmployeeRepository rep = new MyEmployeeRepository();
List<Employee> e = rep.GetAllEmployees();
public class MyEmployeeRepository
{
private string connectionStringVal;
public MyEmployeeRepository()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = ".";
sqlBuilder.InitialCatalog = "LibraryReservationSystem";
sqlBuilder.IntegratedSecurity = true;
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = @"res://*/Test.csdl|res://*/Test.ssdl|res://*/Test.msl";
connectionStringVal = entityBuilder.ToString();
}
public List<Employee> GetAllEmployees()
{
List<Employee> employees = new List<Employee>();
using (var context = new MyEntityDataModelEDM.LibraryReservationSystemEntities1(connectionStringVal))
{
foreach (MyEntityDataModelEDM.Employee p in context.Employees)
{
employees.Add(p);
}
}
return employees;
}
public List<Manager> GetAllManagers()
{
List<Manager> managers = new List<Manager>();
using (var context = new MyEntityDataModelEDM.LibraryReservationSystemEntities1(connectionStringVal))
{
}
return managers;
}
}
修改
这种模式有缺点。它应该考虑以下:
答案 0 :(得分:2)
只是做:
return context.Employees.OfType<Accountant>().ToList()
然而,这是对员工进行建模的一种非常糟糕的方式..如果会计师更改了工作,您需要杀死并重新创建该对象..