How to improve performance with TPT in Entity Framework

I am creating an application using TPT (Table-Per-Type) in Entity Framework 6.

I have these classes

public class Person
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class Customer : Person
{
    public string BillingAddress { get; set; }
}

public class Vendor : Person
{
    public string Address { get; set; }
}

public class IndividualCustomer : Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
}

public class CorporateCustomer : Customer
{
    public string ContactPerson { get; set; }
    public string CorporateTaxNo { get; set; }
}

public class SalesInvoice
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public Customer Customer { get; set; }
}

      

The request is very ugly and takes a long time to generate when I ask for the base class (which is Person and Customer), but quite ok when I request a derived class (IndividualCustomer and CorporateCustomer).

I read that using TPT is very bad for performance and I am considering using TPH (Table-Per-Hierarchy) because of this. But when I use TPH, when adding SalesInvoice, I will need to check if the user is submitting the correct CustomerId. As the database will allow the user to send vendorid as CustomerId. The database will throw an error when using TPT. Thus, TPH will add complexity to the CRUD.

Is there any suggestion on how to improve the TPT query in my case?

Note:

I found that when querying the base class, if I specify which fields I want to get, like Select (x => new {x.Id, x.Name}), the query looks better and does not include the left outer attach derived classes ...

My queries:

//Querying base class
var people = ctx.People.Select(x => new {x.Id, x.Name}).ToList();

//Querying derived class
var corporates = ctx.CorporateCustomers.ToList();

      

+3


source to share





All Articles