Entity Framework Core: Inheritance - Table-per-Type (TPT) is not supported, is it? (Part 2 - Database First)

In the previous post we have created 2 Entity Framework Core (EF Core) models with a code first approach. One model was using the Table-per-Hierarchy (TPH) pattern and the other one Table-per-Type (TPT). In this post we want to approach a more common scenario we see in customer projects: we are using the database first approach now.

All demos are on Github.

Business data model

The business data model is the same as in the previous post. We have 3 DTOs: Person, Customer and Employee.

public class PersonDto
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class CustomerDto : PersonDto
{
    public DateTime DateOfBirth { get; set; }
}

public class EmployeeDto : PersonDto
{
    public decimal Turnover { get; set; }
}

Table-per-Hierarchy (TPH)

We start with the Table-per-Hierarchy pattern. Given is a table People containing all columns from all DTOs incl. 1 column Discriminator to be able to distinguish the customers from employees.

Remark: we are using nvarchar(max) for the sake of simplicity.

TABLE People
(
    Id uniqueidentifier NOT NULL PRIMARY KEY,
    FirstName nvarchar(max) NULL,
    LastName nvarchar(max) NULL,
    DateOfBirth datetime2(7) NULL,
    Turnover decimal(18, 2) NULL,
    Discriminator nvarchar(max) NOT NULL
)

With the following command we let EF Core scaffold the entities (or rather the entity) and the database context:

dotnet ef dbcontext scaffold "Server=(local);Database=TphDemo;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -f -c ScaffoldedTphDbContext --context-dir ./TphModel/DatabaseFirst -o ./TphModel/DatabaseFirst -p ./../../EntityFramework.Demo.csproj -s ./../../EntityFramework.Demo.csproj

The result is not the one we might have expected but is pretty reasonable. The scaffolding creates just 1 entity People with all fields in it because there is no way for EF Core to guess that the table contains 3 entities and not just 1.

public class People
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public decimal? Turnover { get; set; }
public string Discriminator { get; set; }
}

First, let's fix the name of the entity because the name should be Person not People.

For that we create a class that does the pluralization/singularization and register it with the so-called IDesignTimeServices. The implementation of IDesignTimeServices doesn't need any kind of registration, EF Core will find it automatically. The actual pluralization/singularization will be done by the 3rd party-library Inflector.

public class Pluralizer : IPluralizer
{
    public string Pluralize(string identifier)
    {
        // Inflector needs some help with "People" otherwise we get "Peoples"
        if (identifier == "People")
            return identifier;

        return Inflector.Inflector.Pluralize(identifier);
}

    public string Singularize(string identifier)
    {
        return Inflector.Inflector.Singularize(identifier);
    }
}

public class DesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        services.AddSingleton<IPluralizer, Pluralizer>();
    }
}

Now, the generated entity gets the name Person - but to make the model right we have to split the class in 3, manually. After manual adjustments we have 2 options: switch to code first approach or adjust the classes manually after every scaffolding to apply the changes from database. The adjusted code is virtually identical to the one of code first approach but this time the Descriminator is defined explicitly.

Remark: I've renamed Person to PersonTph so the names are the same as in the previous blog post.

public class PersonTph
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Discriminator { get; set; }
}
public class CustomerTph : PersonTph
{
public DateTime DateOfBirth { get; set; }
}
public class EmployeeTph : PersonTph
{
public decimal Turnover { get; set; }
}

The generated database context needs some adjustments as well because DbSets for customers and employees are missing and the field Discriminator has to be defined as one.

public partial class ScaffoldedTphDbContext : DbContext
{
    public virtual DbSet<Person> People { get; set; }

    public ScaffoldedTphDbContext(DbContextOptions<ScaffoldedTphDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Discriminator).IsRequired();
});
    }
}

As with the entities, the only change - compared to code first approach - is the explicit definition of the Discriminator.

public class ScaffoldedTphDbContext : DbContext
{
public virtual DbSet<PersonTph> People { get; set; }
public virtual DbSet<CustomerTph> Customers { get; set; }
public virtual DbSet<EmployeeTph> Employees { get; set; }

public ScaffoldedTphDbContext(DbContextOptions<ScaffoldedTphDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PersonTph>(entity => entity.Property(e => e.Id).ValueGeneratedNever());
modelBuilder.Entity<PersonTph>()
.HasDiscriminator(person => person.Discriminator)
.HasValue<PersonTph>(nameof(PersonTph))
.HasValue<CustomerTph>(nameof(CustomerTph))
.HasValue<EmployeeTph>(nameof(EmployeeTph));
}
}

Table-per-Type (TPT)

Having a database using the TPT pattern we start off 3 with tables:

TABLE People
(
    Id uniqueidentifier NOT NULL PRIMARY KEY,
    FirstName nvarchar(max) NULL,
    LastName nvarchar(max) NULL
)
TABLE Customers
(
    Id uniqueidentifier NOT NULL
        PRIMARY KEY
        FOREIGN KEY REFERENCES People (Id),
    DateOfBirth datetime2(7) NOT NULL
)
TABLE Employees
(
    Id uniqueidentifier NOT NULL
        PRIMARY KEY
        FOREIGN KEY REFERENCES People (Id),
    Turnover [decimal](18, 2) NOT NULL
)

With the following command we create the entities and the database context:

dotnet ef dbcontext scaffold "Server=(local);Database=TptDemo;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -f -c ScaffoldedTptDbContext --context-dir ./TptModel/DatabaseFirst -o ./TptModel/DatabaseFirst -p ./../../EntityFramework.Demo.csproj -s ./../../EntityFramework.Demo.csproj

The scaffolder generates 3 entities that are almost correct. The only flaw is the name of the navigational property IdNavigation pointing to the base class Person.

public partial class Person
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Customer Customer { get; set; }
    public Employee Employee { get; set; }
}
public partial class Employee
{
    public Guid Id { get; set; }
    public decimal Turnover { get; set; }
    public Person IdNavigation { get; set; }
}
public partial class Customer
{
    public Guid Id { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Person IdNavigation { get; set; }
}

Luckily, this issue is very easy to fix by implementing ICandidateNamingService and registering it with IDesignTimeServices.

public class CustomCandidateNamingService : CandidateNamingService
{
    public override string GetDependentEndCandidateNavigationPropertyName(IForeignKey foreignKey)
    {
     if(foreignKey.PrincipalKey.IsPrimaryKey())
            return foreignKey.PrincipalEntityType.ShortName();

        return base.GetDependentEndCandidateNavigationPropertyName(foreignKey);
}
}

public class DesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        services.AddSingleton<IPluralizer, Pluralizer>()
            .AddSingleton<ICandidateNamingService, CustomCandidateNamingService>();
    }
}

After re-running the scaffolder, we get the expected results:

public class Customer
{
public Guid Id { get; set; }
public DateTime DateOfBirth { get; set; }

public Person Person { get; set; }
}
public partial class Employee
{
public Guid Id { get; set; }
public decimal Turnover { get; set; }

public Person Person { get; set; }
}

The last part is the database context. Fortunately, we don't have to change anything.

public partial class ScaffoldedTptDbContext : DbContext
{
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Person> People { get; set; }

public ScaffoldedTptDbContext(DbContextOptions<ScaffoldedTptDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();

entity.HasOne(d => d.Person)
.WithOne(p => p.Customer)
.HasForeignKey<Customer>(d => d.Id);
});

modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();

entity.HasOne(d => d.Person)
.WithOne(p => p.Employee)
.HasForeignKey<Employee>(d => d.Id);
});

modelBuilder.Entity<Person>(entity =>
                        {
                         entity.Property(e => e.Id).ValueGeneratedNever());
                        });
}
}

With TPT we can but don't have to switch to code first approach because we can regenerate the entities and the database context at any time.

Conclusion

Database first approach works best with TPT, with TPH not so much because a relational database knows nothing about any inheritance. With TPT there is just one minor issue but thanks to the great job of the Entity Framework team we can adjust the code generation as we want without the need to copy all the code of Entity Framework Core.


Entity Framework Core: Inheritance - Table-per-Type (TPT) is not supported, is it? (Part 1 - Code First)

With O/R mappers there are a few patterns how a class hierarchy can be mapped to a relational database. The most popular ones are the Table-Per-Hierarchy (TPH) and the Table-Per-Type (TPT) patterns. The Entity Framework Core 2.x (EF Core) officially supports the Table-per-Hierarchy pattern only. The support of Table-per-Type is in the backlog of the Entity Framework team, i.e. it is not (officially) supported yet. Nevertheless, you can use TPT with the current version of EF Core. The usability is not ideal but acceptable. Especially, if you have an existing database using TPT then this short blog post series may give you an idea how to migrate to EF Core.

In the 1st part we will set up 2 EF Core models incl. database migrations for TPH and TPT using code first approach. In the 2nd part we are going to use the database first approach.

Remarks: this blog post is not about what approach is the best for your solution :)

All demos are on Github.

Business data model

In both cases we are going to use the following business data model. For our outward-facing interface, we are using DTOs. We have a PersonDto with 3 fields and 2 derived classes CustomerDto and EmployeeDto, both having 1 additional field.

public class PersonDto
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerDto : PersonDto
{
  public DateTime DateOfBirth { get; set; }
}

public class EmployeeDto : PersonDto
{
  public decimal Turnover { get; set; }
}

Table-Per-Hierarchy (TPH)

Now, let's look at the solution to have internal entities based on TPH. At first, we need to define the entity classes. Thanks to the native support of TPH and the very simple data model the entities are identical to the DTOs.

public class PersonTph
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerTph : PersonTph
{
public DateTime DateOfBirth { get; set; }
}
public class EmployeeTph : PersonTph
{
public decimal Turnover { get; set; }
}

We can implement the database context to be able to access customers and employees like this:

public class TphDbContext : DbContext
{
public DbSet<PersonTph> People { get; set; }
public DbSet<CustomerTph> Customers { get; set; }
public DbSet<EmployeeTph> Employees { get; set; }

public TphDbContext(DbContextOptions<TphDbContext> options)
: base(options)
{
}
}

And for the sake of completion we will be using Entity Framework Core Migrations to create and update the database schema. For that we execute the following command:

dotnet ef migrations add Initial_TPH_Migration -p ./../../EntityFramework.Demo.csproj -s ./../../EntityFramework.Demo.csproj -c TphDbContext -o ./TphModel/CodeFirst/Migrations

As expected we have 1 table with all fields from person, customer and employee and 1 additional column Descriminator, so EF Core is able to differentiate customers from employees.

public partial class Initial_TPH_Migration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable("People",
table => new
{
Id = table.Column<Guid>(nullable: false),
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
DateOfBirth = table.Column<DateTime>(nullable: true),
Turnover = table.Column<decimal>(nullable: true),
Discriminator = table.Column<string>(nullable: false)
},
constraints: table => table.PrimaryKey("PK_People", x => x.Id));
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("People");
}
}

The usage of TPH is nothing special, we just use the appropriate property on the TphDbContext.

TphDbContext ctx = ...

// Create a customer
ctx.Customers.Add(new CustomerTph()
{
Id = Guid.NewGuid(),
FirstName = "John",
LastName = "Foo",
DateOfBirth = new DateTime(1980, 1, 1)
});

// Fetch all customers
var customers = ctx.Customers
    .Select(c => new CustomerDto()
    {
         Id = c.Id,
         FirstName = c.FirstName,
         LastName = c.LastName,
         DateOfBirth = c.DateOfBirth
     })
    .ToList();

Table-Per-Type (TPT) 

Ok, that was easy. Now, how can a solution for TPT look like? With the absence of native support for TPT the entities do not derive from each other but reference each other. The field Id of customer and employee is the primary key and a foreign key pointing to person. The structure of the entities is very similar to the database schema of the TPT pattern.

public class PersonTpt
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerTpt
{
[ForeignKey(nameof(Person))]
public Guid Id { get; set; } // PK and FK pointing to PersonTpt
public PersonTpt Person { get; set; }

public DateTime DateOfBirth { get; set; }
}
public class EmployeeTpt
{
[ForeignKey(nameof(Person))]
public Guid Id { get; set; } // PK and FK pointing to PersonTpt
public PersonTpt Person { get; set; }

public decimal Turnover { get; set; }
}

The database context of TPT is identical to the one of TPH.

public class TptDbContext : DbContext
{
public DbSet<PersonTpt> People { get; set; }
public DbSet<CustomerTpt> Customers { get; set; }
public DbSet<EmployeeTpt> Employees { get; set; }

public TptDbContext(DbContextOptions<TptDbContext> options)
: base(options)
{
}
}

Next, we will create an EF Core migration with the following command

dotnet ef migrations add Initial_TPT_Migration -p ./../../EntityFramework.Demo.csproj -s ./../../EntityFramework.Demo.csproj -c TptDbContext -o ./TptModel/CodeFirst/Migrations

The migration creates 3 tables with correct columns, primary keys and foreign keys.

public partial class Initial_TPT_Migration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable("People",
                        table => new
{
Id = table.Column<Guid>(nullable: false),
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true)
},
constraints: table => table.PrimaryKey("PK_People", x => x.Id));

migrationBuilder.CreateTable("Customers",
table => new
{
Id = table.Column<Guid>(nullable: false),
DateOfBirth = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Customers", x => x.Id);
table.ForeignKey("FK_Customers_People_Id",
                                                    x => x.Id,
                                                    "People",
                                                    "Id",
                                                    onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateTable("Employees",
table => new
{
Id = table.Column<Guid>(nullable: false),
Turnover = table.Column<decimal>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Employees", x => x.Id);
table.ForeignKey("FK_Employees_People_Id",
                                                    x => x.Id,
                                                    "People",
                                                    "Id",
                                                    onDelete: ReferentialAction.Cascade);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("Customers");
migrationBuilder.DropTable("Employees");
migrationBuilder.DropTable("People");
}
}

The biggest difference - compared to TPH - is in the usage of the entities. To get to the fields of the person (i.e. the base type) we have to use the navigational property Person. This may seem cumbersome at first, but it is not a hindrance in practice.

TptDbContext ctx = ...

// Fetch all customers
var customers = ctx.Customers
.Select(c => new CustomerDto()
{
Id = c.Id,
FirstName = c.Person.FirstName,
LastName = c.Person.LastName,
DateOfBirth = c.DateOfBirth
})
.ToList();

// Create a customer
ctx.Customers.Add(new CustomerTpt()
        {
        Person = new PersonTpt()
           {
             Id = Guid.NewGuid(),
             FirstName = "John",
             LastName = "Foo"
             },
DateOfBirth = new DateTime(1980, 1, 1)
});

 Voila!

Conclusion

With Entity Framework Core we can use both the Table-Per-Hierarchy and Table-Per-Type patterns. At least with a code first approach. Whether and how the patterns are applicable using the database first approach we will see in the next blog post.

Stay tuned.


Entity Framework Core 2.1 Performance: Beware of N+1 Queries (Revisited)

In the previous post we have identified some Entity Framework (EF) LINQ queries that are affected by so called N+1 queries problem. In the meantime a new version (2.1-RC1) of Entity Framework has been released so we check the SQL statement generation yet another time.

Samples: Github-Repo 

Positive thing(s) first...

In the previous version the selection of a filtered collection was affected by the problem - with and without ToList() but not anymore

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
Products = g.Products.Where(p => p.Name.Contains("1")).ToList()
})
.ToList();

Adding ToList() leads to 2 SQL statements instead of N+1 where N is the number of selected product groups.

1 query for fetching of the product groups:

SELECT
    [g].[Id], [g].[Name]
FROM
    [ProductGroups] AS [g]
WHERE
    CHARINDEX(N'Group', [g].[Name]) > 0

And 1 query for fetching of the products:

SELECT
    [g.Products].[Id], [g.Products].[GroupId], [g.Products].[Name], [t].[Id]
FROM
    [Products] AS [g.Products]
    INNER JOIN
    (
        SELECT
            [g0].[Id]
        FROM
            [ProductGroups] AS[g0]
        WHERE
            CHARINDEX(N'Group', [g0].[Name]) > 0
    ) AS [t]
        ON [g.Products].[GroupId] = [t].[Id]
WHERE
    CHARINDEX(N'1', [g.Products].[Name]) > 0
ORDER BY
    [t].[Id]

Alas, the usage of FirstOrDefault() is still producing N+1 queries

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
Product = g.Products.FirstOrDefault()
})
.ToList();

and at the moment GroupBy() is not as powerful as in EF 6 so the following query fetches the whole table instead of the first product for each product group.

var firstProducts = Context.Products
.GroupBy(p => p.GroupId)
.Select(g => g.FirstOrDefault())
.ToList();

The corresponding SQL statement is:

SELECT
    [p].[Id], [p].[GroupId], [p].[Name]
FROM
    [Products] AS [p]
ORDER BY
    [p].[GroupId]

 

There is a lot of work to do but we are getting there... until then keep using your favorite profiling tool.


Entity Framework Core Performance: Beware of N+1 Queries

After working with Entity Framework 6 (EF 6) for several years, a software developer can predict the SQL statements being generated by EF just by looking at the LINQ queries. With Entity Framework Core (EF Core) the SQL statement generation has changed - in some cases for the better, in others for the worse.

In this blog post we will check a few LINQ queries and see which of them are executing N+1 SQL statements where N is the number of selected records.

Given is a DbContext with 2 entities Product and ProductGroup. (Repo with sample code: github.com/PawelGerr/Presentation-EntityFrameworkCore)

public class DemoDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductGroup> ProductGroups { get; set; }
}
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }

public Guid GroupId { get; set; }
public ProductGroup Group { get; set; }
}
public class ProductGroup
{
public Guid Id { get; set; }
public string Name { get; set; }

public ICollection<Product> Products { get; set; }
}

Let's print out all product groups having the word "Group" in their names with corresponding products via Include() first and using Select() second.

// Using Include()
var groups = Context.ProductGroups
.Include(g => g.Products)
.Where(g => g.Name.Contains("Group"))
.ToList();

Print(groups);
// Using Select() var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
g.Products
})
.ToList();

Print(groups);

In both cases 2 SQL statements are executed by EF Core: 1 for the product groups and 1 for the products. On the contrary, EF  6 executes just 1 statement. This may imply that the performance of EF 6 is better than the of EF Core, but in practice it is worse because the queries are getting huge and produce more load on the database.

-- Fetching product groups
SELECT [g].[Id], [g].[Name]
FROM [ProductGroups] AS [g]
WHERE CHARINDEX(N'Group', [g].[Name]) > 0
ORDER BY [g].[Id]
-- Fetching products
SELECT [g.Products].[Id], [g.Products].[GroupId], [g.Products].[Name]
FROM [Products] AS [g.Products]
INNER JOIN
(
    SELECT [g0].[Id]
    FROM [ProductGroups] AS [g0]
    WHERE CHARINDEX(N'Group', [g0].[Name]) > 0
) AS [t] ON [ g.Products].[GroupId] = [t].[Id]
ORDER BY [t].[Id]

Now we don't take all products but only those with the term "1"  in their names and print them out twice(!). 

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
Products = g.Products.Where(p => p.Name.Contains("1"))
})
.ToList();

Print(groups); // 1st iteration over product groups
Print(groups); // 2nd iteration over product groups

The result is disappointing. Having 5 product groups matching the condition we get 11 SQL statement executions: 1 query for fetching 5 product groups and (2 * 5=10) for fetching the products. Let's put a ToList() at the end of the products query.

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
Products = g.Products.Where(p => p.Name.Contains("1")).ToList()
})
.ToList();

Now we have 6 (=1+5)  queries being sent to the database, it is getting better but still not satisfying.

-- 1 query for fetching product groups
SELECT [g].[Id], [g].[Name]
FROM [ProductGroups] AS [g]
WHERE CHARINDEX(N'Group', [g].[Name]) > 0
-- 5 queries for fetching products (i.e. 1 query per fetched product group)
SELECT [p].[Id], [p].[GroupId], [p].[Name]
FROM [Products] AS [p]
WHERE (CHARINDEX(N''1'', [p].[Name]) > 0) AND
(@_outer_Id = [p].[GroupId])

Obviously, EF Core has some difficulties translating queries if Select() contains a filtered collection. Let's select just the first product.

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.Select(g => new
{
ProductGroup = g,
Product = g.Products.FirstOrDefault()
})
.ToList();

Print(groups);

We still getting 6 queries meaning that the "problem" doesn't lie in the cardinality of the response type (Product vs ICollection<Product>) but in collections in general.

Solutions

We can reduce the number of queries by not using the navigational property Products but doing the "JOIN" by ourselves, for example via GroupJoin.

var productsQuery = Context.Products.Where(i => i.Name.Contains("1"));

var groups = Context.ProductGroups
.Where(g => g.Name.Contains("Group"))
.GroupJoin(productsQuery, g => g.Id, p => p.GroupId, (g, p) => new
{
ProductGroup = g,
Products = p
})
.ToList();

Print(groups);

The previous LINQ query produces just 1 query.

SELECT [g].[Id], [g].[Name], [t].[Id], [t].[GroupId], [t].[Name]
FROM [ProductGroups] AS [g]
LEFT JOIN
(
    SELECT [i].[Id], [i].[GroupId], [i].[Name]
    FROM [Products] AS [i]
    WHERE CHARINDEX(N'1', [i].[Name]) > 0
) AS [t] ON [g].[Id] = [t].[GroupId]
WHERE CHARINDEX(N'Group', [g].[Name]) > 0
ORDER BY [g].[Id]

An alternative solution is to fetch the data separately and doing lookups in .NET.

var groupsQuery = Context.ProductGroups
.Where(g => g.Name.Contains("Group"));

var productsByGroupId = groupsQuery
                           .SelectMany(g => g.Products.Where(i => i.Name.Contains("1")))
.ToLookup(p => p.GroupId);

var groups = groupsQuery
.Select(g => new
{
ProductGroup = g,
Products = productsByGroupId[g.Id]
})
.ToList();

The generated SQL statements are easier to handle by the database but there are 2 of them. 

-- For product groups
SELECT [g].[Id], [g].[Name]
FROM [ProductGroups] AS [g]
WHERE CHARINDEX(N'Group', [g].[Name]) > 0
-- For products
SELECT [g.Products].[Id], [g.Products].[GroupId], [g.Products].[Name]
FROM [ProductGroups] AS [g]
INNER JOIN [Products] AS [g.Products]
    ON [g].[Id] = [g.Products].[GroupId]
WHERE (CHARINDEX(N'Group', [g].[Name]) > 0) AND
     (CHARINDEX(N'1', [g.Products].[Name]) > 0)

Depending on the database model, amount of data, indexes, number of collections and columns being fetched the one or the other solution may perform better.

Closing Words

The query generation of EF Core is not optimal yet but the Entity Framework Team is currently working on the "N+1 queries" problem so we will re-check all queries with EF Core 2.1 very soon.

In general, whether it is EF 6, EF Core or other O/R mapper it is recommended to use a database profiling tool, so we get good understanding of the technology we use yet again.

 


(ASP).NET Core in production: Changing log level temporarily - 2nd approach

In the previous blog post I talked about how to change the log level at runtime by coupling the appsettings.json (or rather the IConfiguration) with the ILogger. However, the solution has one drawback: you need to change the file appsettings.json for that. In this post we will be able to change the log level without changing the configuration file.

Want to see some real code? Look at the examples on https://github.com/PawelGerr/Thinktecture.Logging.Configuration 

or just use Nuget packages: Thinktecture.Extensions.Logging.Configuration and Thinktecture.Extensions.Serilog.Configuration 

At first we need a custom implementation of IConfigurationSource and IConfigurationProvider. The actual work does the implementation of IConfigurationProvider. The IConfigurationSource is just to inject the provider into your ConfigurationBuilder.

var config = new ConfigurationBuilder()
    .Add(new LoggingConfigurationSource())
    .Build();
----------------------------------------- public class LoggingConfigurationSource : IConfigurationSource
{
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        // Our implementation of IConfigurationProvider
        return new LoggingConfigurationProvider();
    }
}

As we can see, the LoggingConfigurationSource doesn't do pretty much, let us focus on LoggingConfigurationProvider or rather on the interface IConfigurationProvider.

public interface IConfigurationProvider
{
    bool TryGet(string key, out string value);
    void Set(string key, string value);
    IChangeToken GetReloadToken();
    void Load();
    IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath);
}

There are 2 methods that look promising: Set(key, value) for setting a value for a specific key and GetReloadToken() to notify other components (like the logger) about changes in the configuration. Now that we know how to change the configuration values, we need to know the keys and values the logger uses to configure itself. Use Microsoft docs for a hint for Microsoft.Extensions.Logging.ILogger or Serilog.Settings.Configuration in case you are using Serilog.

The pattern for MS-logger key is <<Provider>>:LogLevel:<<Category>>. Here are some examples for the logs coming from Thinktecture components: Console:LogLevel:Thinktecture or LogLevel:Thinktecture.  The value is just one of the Microsoft.Extensions.Logging.LogLevel, like Debug.

namespace Thinktecture
{
    public class MyComponent
    {
        public MyComponent(ILogger<MyComponent> logger)
        {
            logger.LogDebug("Log from Thinktecture.Component");
        }
    }
}

Let's look at the implementation, luckily there is a base class we can use.

public class LoggingConfigurationProvider : ConfigurationProvider
{
    public void SetLevel(LogLevel level, string category = null, string provider = null)
    {
        // returns something like "Console:LogLevel:Thinktecture"
        var path = BuildLogLevelPath(category, provider);
        var levelName = GetLevelName(level); // returns log level like "Debug"

        // Data and OnReload() are provided by the base class
        Data[path] = levelName;
        OnReload(); // notifies other components
    }

    ...
}

Actually, that's it ... You can change the configuration just by setting and deleting keys in the dictionary Data and calling OnReload() afterwards. The only part that's left is to get hold of the instance of LoggingConfigurationProvider to be able to call the method SetLevel from outside but I'm pretty sure you don't need any help for that especially having access to my github repo :)

 

The provided solution does what we intended to, but, do we really want that simple filtering of the logs? Image you are using Entity Framework Core (EF) and there are multiple requests that modify some data. One request is able to commit the transaction the other doesn't and throws, say, an OptimisticConcurrencyException. Your code catches the exception and handles it by retrying the whole transaction, with success. Entity Framework logs this error (i.e. the SQL statement, arguments etc.) internally. The question is, should this error be logged by EF as an Error even if it has been handled by our application? If yes then our logs will be full with errors and it would seem as if we have a lot of bugs in our application. Perhaps it would be better to let EF to log its internal errors as Debug, so that this information is not lost and if our app can't handle the exception then we will log the exception as an error.

But that's for another day ...


.NET Core in production: Changing log level temporarily

When running the application in production then the log level is set somewhere between Information and Error. The question is what to do if you or your customer experiences some undesired behavior and the logs with present log level aren't enough to pinpoint the issue.

The first solution that comes to mind is to try to reproduce the issue on a developer's machine with lower log level like Debug. It may be enough to localize the bug but sometimes it isn't. Even if you are allowed to restart the app in production with lower log level, the issue may go away ... temporarily, i.e. the app still has a bug.

Better solution is to change the log level temporarily without restarting the app.

First step is to initialize the logger with the IConfiguration. That way the logger changes the level as soon as you change the corresponding configuration (file).

In this post I will provide 2 examples, one that is using the ILoggingBuilder of the ASP.NET Core and the other example is using Serilog because it is not tightly coupled to ASP.NET Core (but works very well with it!). 

Using ILoggingBuilder:

// the content of appsettings.json
{
    "Logging": {
        "LogLevel": { "Default": "Information" }
    }
}
-----------------------------------------
var config = new ConfigurationBuilder().
    AddJsonFile("appsettings.json", false, true) // reloadOnChange=true
    .Build();
// Setup of ASP.NET Core application WebHostWebHost
    .CreateDefaultBuilder()
    .ConfigureLogging(builder =>
    {
        builder.AddConfiguration(config); // <= init logger
        builder.AddConsole();
    })
    ...

Using Serilog:

// the content of appsettings.json
{
    "Serilog": {
        "MinimumLevel": { "Default": "Information" }
    }
}
-----------------------------------------
var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", false, true) // reloadOnChange=true
    .Build();
var serilogConfig = new LoggerConfiguration()
    .ReadFrom.Configuration(config) // <= init logger
    .WriteTo.Console();

In case you are interested in integration with (ASP).NET Core 

// If having a WebHost
WebHost
    .CreateDefaultBuilder()
    .ConfigureLogging(builder =>
    {
        builder.AddSerilog(serilogConfig.CreateLogger());
    })
    ...;

// If there is no WebHost
var loggerFactory = new LoggerFactory()
    .AddSerilog(serilogConfig.CreateLogger());

At this point the loggers are coupled to IConfiguration or rather to appsettings.json, i.e if you change the level to Debug the app starts emitting debug-messages as well, without restarting it.

This solution has one downside, you need physical access to the appsettings.json. Even if you do, it still would be better to not change the configuration file. What we want is a component that let us set and reset a temporary log level and if this temporary level is not active then the values from appsettings.json should be used. That way you can change the level from the GUI or via an HTTP request against the Web API of your web application.

Luckily, the implementation effort for that feature is pretty low, but that's for another day...


ASP.NET Core: Update to Autofac 4.6.1 recommended - more than a bugfix release

If you are using Autofac in your ASP.NET Core application then I recommend to update Autofac to version 4.6.1. This bugfix release brought a change how child scope handle additional registrations so that some errors like Cannot resolve parameter 'IOptionsFactory<KestrelServerOptions>' just disappear.

With additional registrations I mean the following:

var builder = new ContainerBuilder();

using (var container = builder.Build())
{
    using (var childScope = container.BeginLifetimeScope(innerBuilder =>
        {
            // additional registration that are known by childScope only
            innerBuilder.RegisterType<Foo>().AsSelf();
        }))
    {
        ...
    }
}

When using a child scope during the setup of an ASP.NET Core application having Autofac 4.6.0 or lower then you had to use the IContainer (= root scope) itself or use a workaround to register MVC components. The first option is not recommended because a DI container should be considered as immutable. The second option could be confusing if you don't know the internals of Autofac.

The second options looks like this:

public class Startup
{
    public Startup(ILifetimeScope childScope)
    {
        _childScope = childScope;
    }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // All singleton are "rebased" to "aspNetScopeTag"
        const string aspNetScopeTag = "AspNetScope";

        _aspNetScope = _childScope.BeginLifetimeScope(aspNetScopeTag,
            builder => builder.Populate(services, aspNetScopeTag));

         return new AutofacServiceProvider(_aspNetScope);
    }
    ...
}

 With Autofac 4.6.1 you just call Populate without any confusing parameters like aspNetScopeTag.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    _aspNetScope = _childScope.BeginLifetimeScope(builder => builder.Populate(services));

    return new AutofacServiceProvider(_aspNetScope);
}

 

Want to try it out? I have prepared 2 projects: AspNetCore2_Autofac460 and AspNetCore2_Autofac461 in my github repo. Download the sources and start the applications with dotnet run in the corresponding folder. The first one will raise an error the latter will start the web server successfully.

 

P.S.: Missing context? Why someone would want to provide its own DI container instead of using IServiceCollection and be done with it? Then read my blog post ASP.NET Core in production: Take back control of your web app or the follow-up post ASP.NET Core in production: Graceful shutdown and reacting to aborted requests.


ASP.NET Core in production: Graceful shutdown and reacting to aborted requests

In the previous post "ASP.NET Core in production: Take back control of your web app" I mentioned that getting hold if the dependency injection (DI) is just one step of many to improve the architecture of your web applications. Today well will look into 2 other aspects that are best explained together: graceful shutdown and reacting to aborted requests.

Image you are in the middle of handling of a web request. How do you know that the WebHost is about to shut down or that the client is not interested in the response anymore? Especially the latter will occur more often and without proper handling you end up wasting resources for nothing.

1) If your component is interested in server shutdown only then you can inject IApplicationLifetime and use the property ApplicationStopping that is of type CancellationToken. Most of asynchronous operations support cancellation tokens so you just have to pass the token to them but be prepare for handling the OperationCanceledException and TaskCanceledException.

public DemoController(IApplicationLifetime appLifetime)
{
    _appLifetime = appLifetime;
}     
[HttpGet("MyAction")]
public async Task<IActionResult> MyAction()
{
    CancellationToken token = _appLifetime.ApplicationStopping;
    return await DoSomethingAsync(token);
}

Having a side-free operation like selecting data from database can be cancelled without much effort just pass the CancellationToken to asynchronous methods (like ToListAsync(token)) or check the property CancellationToken.IsCancellationRequested in case you are iterating over some collection or does other synchronous stuff. However, if the operation is critical, the server is shutting down and the component needs a few seconds more to finish then you can delay the shutdown by registering a callback with the CancellationToken.

Use this feature with care, don't hold up the shutdown indefinitely!

_appLifetime.ApplicationStopping.Register(() =>
{
    // server is not going to shutdown     // until the callback is done
});

Note: you can delay the shutdown by overriding the method Dispose of the controller. Using IDisposable-components to finish some stuff works best if you decouple the DI from ASP.NET life cycle like suggested in my previous post because in most cases you want(!) to shut down the WebHost first so that the endpoints are closed and no new requests can come in. Afterwards you can stop your internal processes and dispose of components.

public class DemoController: Controller
{
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        //finish your stuff synchronously
    }
}

2) Being interested in server shutdown only is more of an edge case. Most of the time it doesn't matter whether the operation has to stop because of the shutdown or because the client has aborted the HTTP request (e.g. by navigating to another page). In both cases you can use HttpContext.RequestAborted which is another CancellationToken. Although it is called "request-aborted" it is cancelled on shutdown as well because in this case the request is aborted by the server itself not the client.

[HttpGet("MyAction")]
public async Task<IActionResult> MyAction()
{
    CancellationToken token = HttpContext.RequestAborted;
    return await DoSomethingAsync(token);
}

If the method happens to be an action of an MVC/Web API controller (like in my examples) then you can just specify a new method argument of type CancellationToken to get HttpContext.RequestAborted provided to you.

[HttpGet("MyAction")]
public async Task<IActionResult> MyAction(CancellationToken token)
{
    return await DoSomethingAsync(token);
}

 

Want to try it out? I've updated the example from my previous post so that you are able to start and abort HTTP requests or restart the WebHost. The sources are available on Github.

ASP.NET Core - Graceful shutdown


ASP.NET Core in production: Take back control of your web app

With ASP.NET Core the setup of a new MVC or a Web API project is essentially a 2-liner. The standard setup you can find in most tutorials is ideal for demos, workshops or simple web apps but is insufficient for complex applications.

The standard setup looks as follows:

WebHost
.CreateDefaultBuilder()
.UseStartup<Startup>()
.Build()
.Run();

The rest of the configuration of the web app is usually made in Startup.

The problem with the standard approach is that the WebHost controls your entire web application, i.e. it dictates the app life cycle, sets up dependency injection framework, starts and stops your processes and if the WebHost is shutting down (or crashes) then your web application is down as well. Especially the stopping of the app is more of a crash than a graceful shutdown.

A web application is usually more than just a bunch of endpoints. Depending on your requirements you may need a more complex startup, perhaps consisting of multiple steps like

  • setting up logging first
  • pulling configuration
  • filling caches
  • processing of (temp) data from previous run
  • starting some tasks like listening on the file system or for pulling the requests from a message queue because HTTP is not the only gateway to the world
  • etc.

Only after all preparation steps are made the app is ready to open the HTTP endpoints to accept new requests, i.e. to start the WebHost. The web app may run for weeks or month so you may want to restart it (automatically) multiple times to reconfigure the ASP.NET Core pipeline without killing the whole process. And, if the app has to be stopped then you probably want to close the endpoints first, give running requests some time to finish, process the data in buffers or temp files, log some statistics and only then to stop the application.

The first step to get these features is to get control over dependency injection (DI) managing your components. Giving full control of the DI to the ASP.NET Core runtime means that you can't bootstrap your processes before starting the WebHost and if it is stopped then the DI container incl. all your components is disposed of as well. This makes a graceful shutdown very hard. 

In the following examples I'm using Autofac because it is one of the feature-richest and most mature dependency injection frameworks available for .NET. Especially, with Autofac v4.6.1 and Autofac.Extensions.DependencyInjection is it very easy to achieve the desired setup.

Example of a more complex startup so it is easier to get the idea what I 'm talking about: (pseudo code)

var builder = new ContainerBuilder();
// register all dependencies specific to your app
// not necessarilly specific to ASP.NET ...
using (var container = builder.Build())
{
    // assume, depending on some configurations
    // you need to add further dependencies
    var myConfig = container.Resolve<MyConfiguratio>();

    // Adding further dependencies on a child scope
    // can be repeated multiple times if required
    using (var appScope =
        container.BeginLifetimeScope(appScopeBuilder => RegisterFurtherDeps(myConfig)))
    {
        // All methods should do their work in child scopes of "appScope"
        // which should be disposed eventually to free the resources.

        // For example, check file system permissions, databases etc.
        CheckPreconditions(appScope);
        CleanupTheMessFromPreviousRun(appScope);
        StartSomeKindOfTasks(appScope);

        await StartWebHostAsync(appScope);

        // when we are here then that means the endpoints are closed
        WaitForPendingRequests(maxWaitTime: Timespan.FromSeconds(5));
        Cleanup();
        LogStatistics();
    }
}

....

public async Task StartWebHostAsync(ILifetimeScope appScope)
{
    // The WebHost gets its own scope and can do with it what ever it wants,
    // like "polluting" it with some ASP.NET stuff :)
    using (var webHostScope =
        appScope.BeginLifetimeScope(builder => RegisterAspNetSpecificStuff(builder)))
    {
        await WebHost
        .CreateDefaultBuilder()
        .UseStartup<Startup>()
         // This method does not exist but you get the point
        .UseThisDependencyInjectionScope(webHostScope) 
         .Build()
        .RunAsync();
    }
}

 

After a lengthly motivational part let's get to the real code.

In this demo we will setup an MVC web and restart the WebHost on button-click just to proof that the application stays alive even if the web server goes down. 

 At first we set up a DI container and register a class MySingleton as a singleton. This class will be the proof that it is not going to be recreated every time the WebHost restarts.

private static async Task StartWebAppAsync()
{
    var builder = new ContainerBuilder();
    builder.RegisterType<MySingleton>().AsSelf().SingleInstance();
    
    using (var container = builder.Build())
    {
        while (true)
       {
           await StartWebServerAsync(container);
       }
   }
}

The configuration of the WebHost differs just in one point: the instantiation of the class Startup is delegated to Autofac. Because of that our Startup is getting ILifetimeScope via constructor injection.

private static async Task StartWebServerAsync(ILifetimeScope scope)
{
    using (var webHostScope = scope.BeginLifetimeScope(
        builder => builder.RegisterType<Startup>().AsSelf()))
   {
       await WebHost
        .CreateDefaultBuilder()
        .UseStartup<Startup>()
        .ConfigureServices(services =>
            services.AddTransient(provider => webHostScope.Resolve<Startup>()))
        .Build()
        .RunAsync();
   }
}

Note: If your Startup has some ASP.NET specific dependencies, say IHostingEnvironment and IConfiguration then you have to pull them out of IServiceProvider and pass them to a factory created by Autofac.

public Startup(ILifetimeScope webHostScope, 
    IHostingEnvironment hostingEnvironment,
    IConfiguration configuration)
{
    ...
}

...
await WebHost
    ...
    .ConfigureServices(services => services.AddTransient(provider =>
    {
        var hostingEnv = provider.GetRequiredService<IHostingEnvironment>();
        var config = provider.GetRequiredService<IConfiguration>();
        var factory = webHostScope.Resolve<Func<IHostingEnvironment, IConfiguration, Startup>>();          return factory(hostingEnv, config);
    }))
    ...

Ok, back on track.  

In the Startup we copy the DI registrations from IServiceCollection to our DI container by using the method Populate and save the newly created DI scope in a variable so it can be disposed when the ASP.NET part is shutting down.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // register ASP.NET Core stuff
    services.AddMvc();
    _aspNetScope = _webHostScope.BeginLifetimeScope(builder => builder.Populate(services));     return new AutofacServiceProvider(_aspNetScope);
}
public void Configure(IApplicationBuilder app, IApplicationLifetime appLifetime)
{
    app.UseMvc();
    appLifetime.ApplicationStopped.Register(() => _aspNetScope.Dispose());
}

 

Your MVC application is now set up properly.

Want to try it out? Download the sources from Github, launch the mini MVC app with dotnet run, go to the url printed in the console window and start restarting the WebHost :)

ASP.NET Core - Take back the control of your web app

 

P.S.: Getting hold of the DI is just one but not the only one aspect of keeping your app under control. But that's a topic for another day...


ASP.NET Core: Beware - Singleton may not be singleton

If you register a type as a singleton then you expect just 1 instance of this type in your whole application. What you may not know is that ASP.NET Core is creating 2 instances of IServiceProvider during building of the IWebHost that may lead to 2 instance of your "singleton".

This is the case if you register a type, say MySingleton, when configuring the web host ...

WebHost
.CreateDefaultBuilder()
.UseStartup<Startup>()
.ConfigureServices(services => services.AddSingleton<MySingleton>())
.Build()
.Run();

 ..., e.g. so that is available in the constructor of your Startup

public class Startup
{
private readonly MySingleton _mySingletonFromHostingServiceProvider;
public Startup(MySingleton mySingletonFromHostingServiceProvider)
{
_mySingletonFromHostingServiceProvider = mySingletonFromHostingServiceProvider;
}
...
}

Now, if we resolve MySingleton during normal web request we get a whole new instance instead the same instance as in constructor of the Startup class. 

public void Configure(IApplicationBuilder app)
{
app.Use((ctx, next) =>
{
var mySingleton = ctx.RequestServices.GetRequiredService<MySingleton>();

// the comparison of 2 instances yields "false"
var areEqual = _mySingletonFromHostingServiceProvider == mySingleton;

Console.WriteLine($"==> {nameof(_mySingletonFromHostingServiceProvider)} == {nameof(mySingleton)}: {areEqual}");          return next();
    });
}

 There are at least two ways to fix this problem.

Either pass an instance of MySingleton to method AddSingleton instead of passing just the type

var mySingleton = new MySingleton();
WebHost
.CreateDefaultBuilder()
.UseStartup<Startup>()
.ConfigureServices(services => services.AddSingleton(mySingleton))
.Build()
.Run();

 or by replacing the previous registration with a new one in ConfigureServices 

public class Startup
{
private readonly MySingleton _mySingletonFromHostingServiceProvider;
public Startup(MySingleton mySingletonFromHostingServiceProvider)
{
_mySingletonFromHostingServiceProvider = mySingletonFromHostingServiceProvider;
}
public void ConfigureServices(IServiceCollection services)
{
services.Replace(new ServiceDescriptor(typeof(MySingleton), _mySingletonFromHostingServiceProvider));
  // alternative way
//services.AddSingleton(_mySingletonFromHostingServiceProvider);
}
...
}

 

According to @davidfowl the ASP.NET team will address this problem in the future. 

PS: There is at least another one solution to fix this problem and gaining back the control over your web app but that's for another time ... :)