I am trying to find examples or documentation related to DbMigration and ColumnModel. I simply want to specify width of string propery in DBMigration Up method E.g.
AddColumn("Districts", "Code", c => c.String());
will create nvarchar(max) - and I want to specify maxlength(20) for example. Is this integrated with EntityTypeConfiguration or I have to add also manually
this.Property(t => t.Name).HasColumnName("Code").IsRequired().HasMaxLength(20);
MSDN help doesn't give any examples and walkthrough on ef4 blog covers only basics
If you use AddColumn directly you can simply use:
AddColumn("Districts", "Code", c => c.String(maxLength: 20));
If you define max length in EntityTypeConfiguration (which is correct approach) EF Migration will handle it for you.
The configuration of the columns should be done as data annotations. To make a string column have a specific width, use the StringLengthAttribute:
[Required] // Makes column not null
[StringLength(20)] // Makes it a nvarchar(20) instead of nvarchar(max)
public string SomeString {get;set;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With