Code-First モデルに Table-Per-Hierarchy (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);
私が本当に必要としているのは、ComboBox を埋めるための、データベースからの Customers テーブルの生のコンテンツだけです。EntityFramework のみを使用してよりクリーンな方法はありませんか?