0

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 のみを使用してよりクリーンな方法はありませんか?

4

1 に答える 1

1

最初にコードで TPH を使用している場合、基本的な方法は、要素の型が基本型である階層全体に対して 1 つの DbSet のみを持つことです。次に.Map、ベース エンティティの派生型の条件を設定するために使用します。派生型をクエリするとき.OfType<T>()は、1 つのエンティティ セットに対してメソッドを使用して、派生型でフィルター処理します。次の宣伝文句は例を示しています。

https://msdn.microsoft.com/en-us/data/jj591617#2.4

于 2015-09-14T20:04:05.720 に答える