Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document SQL Server sparse columns #3456

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
roji marked this conversation as resolved.
Show resolved Hide resolved

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.
roji marked this conversation as resolved.
Show resolved Hide resolved

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>