[C#] Defining Column Types and Constraints with Data Annotations

In Entity Framework Core, you can use “Data Annotations” (attributes) on your entity class properties. This allows you to control the details of the database table definition, such as data types, character limits, and NULL constraints.

This article uses an “Employee Master” table as an example to explain how to set fixed-length strings, decimal precision, and specific date types.

目次

Implementation Sample: Defining an Employee Master Table

The following code uses the [Column] and [MaxLength] attributes on the Employee class. This ensures that the database creates the intended types on servers like SQL Server or MySQL.

Model.cs

using System;
using System.ComponentModel.DataAnnotations;       // For [Required], [MaxLength], etc.
using System.ComponentModel.DataAnnotations.Schema; // For [Column], etc.

public class Employee
{
    // ID (Primary Key)
    public int EmployeeId { get; set; }

    // [Required]: Sets a NOT NULL constraint
    // [Column]: Specifies "char(10)" to define it as a fixed 10-digit code
    [Required]
    [Column(TypeName = "char(10)")]
    public string EmployeeCode { get; set; }

    // [MaxLength]: Limits the string to 50 characters (e.g., nvarchar(50) in DB)
    [Required]
    [MaxLength(50)]
    public string FullName { get; set; }

    // Explicitly specify precision for decimal types
    // Here, "decimal(12, 2)" means 12 digits total with 2 decimal places
    [Column(TypeName = "decimal(12, 2)")]
    public decimal Salary { get; set; }

    // Force the DB to use "date" (date only) instead of the default DateTime (date + time)
    [Column(TypeName = "date")]
    public DateTime HireDate { get; set; }

    // Use DateTime? for optional dates and specify the "date" type
    [Column(TypeName = "date")]
    public DateTime? ResignationDate { get; set; }
}

Explanation of Key Attributes

1. [Column(TypeName = “…”)]

This attribute directly specifies the database-specific type name.

  • char(n) / varchar(n): Use this when you want fixed-length strings or specific character sets.
  • decimal(p, s): This is essential for currency or scientific calculations to ensure the correct number of digits and precision.
  • date: Use this for columns that do not need time information, such as birthdays or hire dates. It saves storage space and prevents accidental time data entry.

2. [MaxLength(n)]

This defines the maximum length of string data. In the database, this creates columns like varchar(n) or nvarchar(n). It also acts as a validation rule for input checks within your application.

3. [Required]

This indicates that the property must have a value. It adds a NOT NULL constraint in the database. While value types like int are NOT NULL by default, use this attribute for reference types like string when you want to make them mandatory.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次