Skip to content

Commit

Permalink
Add a sample showing how to configure scale and precision
Browse files Browse the repository at this point in the history
Fixes #527
  • Loading branch information
AndriySvyryd authored and divega committed Oct 18, 2017
1 parent 4c6e689 commit 8f90598
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
20 changes: 7 additions & 13 deletions entity-framework/core/modeling/relational/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ For example, SQL Server uses `datetime2(7)` for `DateTime` properties, and `nvar

## Data Annotations

You can use Data Annotations to specify an exact data type for the column.

[!code-csharp[Main](../../../../samples/core/Modeling/DataAnnotations/Samples/Relational/DataType.cs?name=Entities&highlight=4)]
``` csharp
public class Blog
{
public int BlogId { get; set; }
[Column(TypeName = "varchar(200)")]
public string Url { get; set; }
}
```
You can use Data Annotations to specify an exact data type for a column.

For example the following code configures `Url` as a non-unicode string with maximum length of `200` and `Rating` as decimal with precision of `5` and scale of `2`.

[!code-csharp[Main](../../../../samples/core/Modeling/DataAnnotations/Samples/Relational/DataType.cs?name=Entities&highlight=4,6)]

## Fluent API

You can use the Fluent API to specify an exact data type for the column.
You can also use the Fluent API to specify the same data types for the columns.

[!code-csharp[Main](../../../../samples/core/Modeling/FluentAPI/Samples/Relational/DataType.cs?name=Model&highlight=7,8,9)]
[!code-csharp[Main](../../../../samples/core/Modeling/FluentAPI/Samples/Relational/DataType.cs?name=Model&highlight=9-10)]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;

namespace EFModeling.Configuring.DataAnnotations.Samples.Relational.DataType
Expand All @@ -14,6 +14,8 @@ public class Blog
public int BlogId { get; set; }
[Column(TypeName = "varchar(200)")]
public string Url { get; set; }
[Column(TypeName = "decimal(5, 2)")]
public decimal Rating { get; set; }
}
#endregion
}
11 changes: 7 additions & 4 deletions samples/core/Modeling/FluentAPI/Samples/Relational/DataType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace EFModeling.Configuring.FluentAPI.Samples.Relational.DataType
{
Expand All @@ -9,16 +9,19 @@ class MyContext : DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasColumnType("varchar(200)");
modelBuilder.Entity<Blog>(eb =>
{
eb.Property(b => b.Url).HasColumnType("varchar(200)");
eb.Property(b => b.Rating).HasColumnType("decimal(5, 2)");
});
}
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public decimal Rating { get; set; }
}
#endregion
}

0 comments on commit 8f90598

Please sign in to comment.