Skip to content

Commit

Permalink
Document SQL Server sparse columns
Browse files Browse the repository at this point in the history
Closes #3017
  • Loading branch information
roji committed Oct 1, 2021
1 parent 60e5eda commit d14a7e3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
22 changes: 22 additions & 0 deletions entity-framework/core/providers/sql-server/columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Microsoft SQL Server Database Provider - Columns - EF Core
description: Column features specific to the Entity Framework Core SQL Server provider
author: roji
ms.date: 10/1/2021
uid: core/providers/sql-server/columns
---
# Column features specific to the Entity Framework Core SQL Server provider

This page details column configuration options that are specific to the SQL Server provider.

## Sparse columns

Sparse columns are ordinary columns that have an optimized storage for null values, reducing the space requirements for null values at the cost of more overhead to retrieve non-NULL values.

As an example, consider a type hierarchy mapped via [the table-per-hierarchy (TPH) strategy](xref:core/modeling/inheritance#table-per-hierarchy-and-discriminator-configuration). In TPH, a single database table is used to hold all types in a hierarchy; this means that the table must contain columns for each and every property across the entire hierarchy, and for columns belonging to rare types, most rows will contain a NULL value for that column. In these cases, it may make sense to configure the column as *sparse*, in order to reduce the space requirements. The decision whether to make a column sparse must be made by the user, and depends on expectations for actual data in the table.

A column can be made sparse via the Fluent API:

[!code-csharp[SparseColumn](../../../../samples/core/SqlServer/Columns/SparseColumnContext.cs?name=SparseColumn&highlight=5)]

For more information on sparse columns, [see the SQL Server docs](/sql/relational-databases/tables/use-sparse-columns).
2 changes: 2 additions & 0 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@
href: core/providers/sql-server/value-generation.md
- name: Function mappings
href: core/providers/sql-server/functions.md
- name: Columns
href: core/providers/sql-server/columns.md
- name: Indexes
href: core/providers/sql-server/indexes.md
- name: Memory-optimized tables
Expand Down
11 changes: 11 additions & 0 deletions samples/core/SqlServer/Columns/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SqlServer.Columns
{
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public DateTime PublishedOn { get; set; }
}
}
7 changes: 7 additions & 0 deletions samples/core/SqlServer/Columns/RareBlog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SqlServer.Columns
{
public class RareBlog : Blog
{
public string RareProperty { get; set; }
}
}
19 changes: 19 additions & 0 deletions samples/core/SqlServer/Columns/SparseColumnContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Columns
{
public class SparseColumnContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<RareBlog> RareBlogs { get; set; }

#region SparseColumn
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RareBlog>()
.Property(b => b.RareProperty)
.IsSparse();
}
#endregion
}
}
4 changes: 2 additions & 2 deletions samples/core/SqlServer/SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-rc.1.21452.10" />
</ItemGroup>

</Project>

0 comments on commit d14a7e3

Please sign in to comment.