-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #3017
- Loading branch information
Showing
6 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters