Skip to content

Commit

Permalink
Fix collation code snippet
Browse files Browse the repository at this point in the history
Fixes #3049
  • Loading branch information
roji committed Jan 28, 2021
1 parent d879b12 commit 8d9e6ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ In most database systems, a default collation is defined at the database level;

When using EF Core migrations to manage your database schema, the following in your model's `OnModelCreating` method configures a SQL Server database to use a case-sensitive collation:

[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?range=40)]
[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?name=DatabaseCollation)]

## Column collation

Collations can also be defined on text columns, overriding the database default. This can be useful if certain columns need to be case-insensitive, while the rest of the database needs to be case-sensitive.

When using EF Core migrations to manage your database schema, the following configures the column for the `Name` property to be case-insensitive in a database that is otherwise configured to be case-sensitive:

[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?name=OnModelCreating&highlight=6)]
[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?name=ColumnCollation)]

## Explicit collation in a query

Expand Down
14 changes: 7 additions & 7 deletions entity-framework/core/modeling/entity-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ You can also configure your columns to specify an exact data type for a column.

### [Data Annotations](#tab/data-annotations)

[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnDataType.cs?name=ColumnDataType&highlight=4,6)]
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnDataType.cs?name=ColumnDataType&highlight=5,8)]

### [Fluent API](#tab/fluent-api)

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/ColumnDataType.cs?name=ColumnDataType&highlight=5-6)]
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/ColumnDataType.cs?name=ColumnDataType&highlight=6-7)]

***

Expand All @@ -70,7 +70,7 @@ In the following example, configuring a maximum length of 500 will cause a colum

#### [Data Annotations](#tab/data-annotations)

[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/MaxLength.cs?name=MaxLength&highlight=4)]
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/MaxLength.cs?name=MaxLength&highlight=5)]

#### [Fluent API](#tab/fluent-api)

Expand Down Expand Up @@ -119,7 +119,7 @@ The following example shows an entity type with required and optional properties

#### [Without NRT (default)](#tab/without-nrt)

[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/CustomerWithoutNullableReferenceTypes.cs?name=Customer&highlight=4-8)]
[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/CustomerWithoutNullableReferenceTypes.cs?name=Customer&highlight=5,8)]

#### [With NRT](#tab/with-nrt)

Expand All @@ -140,7 +140,7 @@ A property that would be optional by convention can be configured to be required

#### [Data Annotations](#tab/data-annotations)

[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/Required.cs?name=Required&highlight=4)]
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/Required.cs?name=Required&highlight=5)]

#### [Fluent API](#tab/fluent-api)

Expand All @@ -155,7 +155,7 @@ A property that would be optional by convention can be configured to be required
A collation can be defined on text columns, determining how they are compared and ordered. For example, the following code snippet configures a SQL Server column to be case-insensitive:

[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?range=42-43)]
[!code-csharp[Main](../../../samples/core/Miscellaneous/Collations/Program.cs?name=ColumnCollation)]

If all columns in a database need to use a certain collation, define the collation at the database level instead.

Expand All @@ -170,7 +170,7 @@ You can set an arbitrary text comment that gets set on the database column, allo
> [!NOTE]
> Setting comments via data annotations was introduced in EF Core 5.0.
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnComment.cs?name=ColumnComment&highlight=4)]
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnComment.cs?name=ColumnComment&highlight=5)]

### [Fluent API](#tab/fluent-api)

Expand Down
6 changes: 4 additions & 2 deletions samples/core/Miscellaneous/Collations/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFCollations;Trusted_Connection=True;ConnectRetryCount=0");
}

#region OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region DatabaseCollation
modelBuilder.UseCollation("SQL_Latin1_General_CP1_CS_AS");
#endregion

#region ColumnCollation
modelBuilder.Entity<Customer>().Property(c => c.Name)
.UseCollation("SQL_Latin1_General_CP1_CI_AS");
#endregion
}
#endregion
}

public class Customer
Expand Down

0 comments on commit 8d9e6ed

Please sign in to comment.