Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify the maximum column length of a field using entity framework code first

Is there an attribute I can use when creating a table ? I tried [StringLength] but it seems to be ignored.

 public class EntityRegister  {      public int Id { get; set; }      [StringLength(450)]      public string Name { get; set; }  } 
like image 655
Kirsten Avatar asked Dec 23 '12 03:12

Kirsten


People also ask

Which of the following annotations specifies the maximum string length allowed by the database column?

Data Annotations - StringLength Attribute in EF 6 & EF Core It specifies the maximum characters allowed for a string property which in turn sets the size of a corresponding column ( nvarchar in SQL Server) in the database.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What is EDMX entity framework?

An . edmx file is an XML file that defines a conceptual model , a storage model , and the mapping between these models. An . edmx file also contains information that is used by the ADO.NET Entity Data Model Designer (Entity Designer) to render a model graphically.


1 Answers

alternatively, you can manually do it on Fluent API

use HasMaxLength(450)

  • Configuring Properties and Types with the Fluent API
    • EF 6
    • EF Core

or if you want Data Annotation, use MaxLength and MinLength attributes

public class EntityRegister {     public int Id { get; set; }     [MaxLength(450)]     public string Name { get; set; } } 
  • Code First Data Annotations
like image 193
John Woo Avatar answered Sep 24 '22 02:09

John Woo