Setting MaxLength for all strings in Entity Framework code first
asp.net-mvc, c#, ef-code-first, entity-framework
Solution
You can do this, which ensures all strings are the maximum length supported by the database provider:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<string>().Configure(p => p.IsMaxLength());
}
Add this method (or modify the existing one) in your `DbContext` class.
Problem
I'm developing a code-first database, using Entity Framework 6. I know I can set `[MaxLength(myLen)]` on the property of a model. What I wondered, is if this is possible to do in a filter or a custom attribute, so that all strings take on a default, of say 250, unless specified directly on the property. Failing this, is there a way to change the default of `nvarchar(max)`?