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

1

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; }
 }
Phraseograph answered 23/12, 2012 at 3:44 Comment(0)
T
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; }
}
Temporary answered 23/12, 2012 at 3:46 Comment(5)
I tried both of these, and dropped the database, but it re-creates with the field being (MAX)Phraseograph
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 <X64> (Build 7601: Service Pack 1)Phraseograph
the version, i think, doesn't matter at all. it's all about EF :DTemporary
its working like you say now, I think I must have had problems with the migrationPhraseograph
[MaxLength] is not recognized. Do I need to include or install something extra for this? @John WooPtolemaist

© 2022 - 2024 — McMap. All rights reserved.