EF 4.3.1 - code first automatic migrations - how to specify column width
I am trying to find examples or documentation related to DbMigration and ColumnModel. I just want to specify the row width in DBMigration Up method For example.
AddColumn("Districts", "Code", c => c.String());
will create nvarchar (max) - and I want to specify maxlength (20) for example. Is it integrated with EntityTypeConfiguration or should I add manually as well
this.Property(t => t.Name).HasColumnName("Code").IsRequired().HasMaxLength(20);
The MSDN Help does not provide any examples and the step-by-step tutorial on the ef4 blog only covers the basics
If you are using AddColumn
directly, you can simply use:
AddColumn("Districts", "Code", c => c.String(maxLength: 20));
If you define a maximum length in EntityTypeConfiguration
(this is the correct approach), EF Migration will handle it for you.
Column configuration should be done as data annotations. To make a column row have a specific width, use StringLengthAttribute
:
[Required] // Makes column not null
[StringLength(20)] // Makes it a nvarchar(20) instead of nvarchar(max)
public string SomeString {get;set;}