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
c # entity-framework entity-framework-6


source to share


No one has answered this question yet

Check out similar questions:

3575
How to list a transfer?
783
Entity Framework vs LINQ to SQL
758
Entity Framework 5 Record Update
756
Failed to check for one or more objects. For more information, see "EntityValidationErrors Property".
630
Fastest way to insert into Entity framework
515
How can I get the ID of the inserted entity in the Entity framework?
461
How do I view the SQL generated by the Entity Framework?
443
SqlException from Entity Framework - new transaction is not allowed because there are other threads in the session
401
Entity Framework Service Provider not found for ADO.NET Provider with invariant name 'System.Data.SqlClient'
392
Failed to load Entity Framework provider type?



All Articles
Loading...
X
Show
Funny
Dev
Pics