我有以下模型对应于下面列出的数据库表。
经理是雇员。会计师也是雇员。
- 在存储库中获取所有管理器的最佳方法是什么?如何实现 GetAllManagers() 方法?
- 它是正确的TPT吗?
代码
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;
}
}
编辑
这个模型有缺点。应考虑以下几点:
- 可以在没有任何角色的情况下创建员工。
- 一名员工可以担任多个角色。