我正在为我的代码优先模型使用按层次结构表 (TPH) 结构。我有一个基本模型客户:
public abstract class Customer : IEntity
{
[Key]
public int Id { get; set; }
protected BusinessType CustomerType { get; set; }
}
我有几个模型继承客户:
public class CommercialCustomer : Customer, IAggregateRoot
{
public CommercialCustomer()
{
CustomerType = BusinessType.Commercial;
}
[DisplayName("Customer Name")]
public string BusinessName { get; set; }
}
public class ResidentialCustomer : Customer, IAggregateRoot
{
public ResidentialCustomer()
{
CustomerType = BusinessType.Residential;
}
[DisplayName("Customer Name")]
public string CustomerName { get; set; }
}
public class EducationalCustomer : Customer, IAggregateRoot
{
public EducationalCustomer()
{
CustomerType = BusinessType.Educational;
}
public string SchoolName { get; set; }
public string Department { get; set; }
}
等等。要获取我使用的住宅客户列表:
IList<ResidentialCustomer> customers = _context.ResidentialCustomers.ToList();
我需要帮助的是如何获得主客户列表作为超集?正如预期的那样,在 Customer 上调用 .ToList() 扩展方法只会返回 Id 属性。使用 SqlDataAdapter 我可以返回一个表...</p>
const string cmdString =
"SELECT Id, CONCAT(SchoolName + ', ' + Department, CustomerName, BusinessName) AS Name FROM Customers ORDER BY Name";
string connectionString = ConfigurationManager.ConnectionStrings["TestContext"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(cmdString, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
我真正需要的只是来自数据库的客户表的原始内容来填充组合框。仅使用 EntityFramework 就没有更清洁的方法吗?