40

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; }
 }
John Woo
  • 258,903
  • 69
  • 498
  • 492
Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

67

alternatively, you can manually do it on Fluent API

use HasMaxLength(450)

  • Configuring Properties and Types with the Fluent API

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; }
}
Al Dass
  • 831
  • 15
  • 23
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • I tried both of these, and dropped the database, but it re-creates with the field being (MAX) – Kirsten Dec 23 '12 at 03:58
  • is it the version of SQL I am using Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64) Mar 29 2009 10:11:52 Copyright (c) 1988-2008 Microsoft Corporation Express Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) – Kirsten Dec 23 '12 at 04:18
  • 1
    the version, i think, doesn't matter at all. it's all about `EF` :D – John Woo Dec 23 '12 at 04:20
  • 1
    its working like you say now, I think I must have had problems with the migration – Kirsten Dec 23 '12 at 04:46
  • [MaxLength] is not recognized. Do I need to include or install something extra for this? @John Woo – Cole Perry Nov 17 '20 at 16:49