diff --git a/entity-framework/core/providers/sql-server/columns.md b/entity-framework/core/providers/sql-server/columns.md new file mode 100644 index 0000000000..e9e1e07d88 --- /dev/null +++ b/entity-framework/core/providers/sql-server/columns.md @@ -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). \ No newline at end of file diff --git a/entity-framework/toc.yml b/entity-framework/toc.yml index 024446d35f..0f504543a8 100644 --- a/entity-framework/toc.yml +++ b/entity-framework/toc.yml @@ -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 diff --git a/samples/core/SqlServer/Columns/Blog.cs b/samples/core/SqlServer/Columns/Blog.cs new file mode 100644 index 0000000000..199235fa31 --- /dev/null +++ b/samples/core/SqlServer/Columns/Blog.cs @@ -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; } + } +} diff --git a/samples/core/SqlServer/Columns/RareBlog.cs b/samples/core/SqlServer/Columns/RareBlog.cs new file mode 100644 index 0000000000..c395c8a460 --- /dev/null +++ b/samples/core/SqlServer/Columns/RareBlog.cs @@ -0,0 +1,7 @@ +namespace SqlServer.Columns +{ + public class RareBlog : Blog + { + public string RareProperty { get; set; } + } +} diff --git a/samples/core/SqlServer/Columns/SparseColumnContext.cs b/samples/core/SqlServer/Columns/SparseColumnContext.cs new file mode 100644 index 0000000000..31145f50c5 --- /dev/null +++ b/samples/core/SqlServer/Columns/SparseColumnContext.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; + +namespace SqlServer.Columns +{ + public class SparseColumnContext : DbContext + { + public DbSet Blogs { get; set; } + public DbSet RareBlogs { get; set; } + + #region SparseColumn + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.RareProperty) + .IsSparse(); + } + #endregion + } +} diff --git a/samples/core/SqlServer/SqlServer.csproj b/samples/core/SqlServer/SqlServer.csproj index b1f0a506b8..c95621cc4b 100644 --- a/samples/core/SqlServer/SqlServer.csproj +++ b/samples/core/SqlServer/SqlServer.csproj @@ -2,11 +2,11 @@ Exe - net5.0 + net6.0 - +