change all string property max length

c# entity-framework entity-framework-core

Question

In EF 6 I can do something like this:

modelBuilder
  .Properties()
  .Where(p => p.PropertyType == typeof(string) && 
              p.GetCustomAttributes(typeof(MaxLengthAttribute), false).Length == 0)
  .Configure(p => p.HasMaxLength(2000));

since EF7 ModelBuilder doesn't have the Properties() function, how do I do same thing in EF7?

1
7
1/2/2017 12:39:50 PM

Accepted Answer

I suppose this to be one of the "still lacking" functionalities in EF Core and expect it to be added in some later version.

Until then, the closest I can suggest (for v1.1.0) is as follows:

foreach (var p in modelBuilder.Model
    .GetEntityTypes()
    .SelectMany(t => t.GetProperties())
    .Where(p => p.ClrType == typeof(string) && p.GetMaxLength() == null))
{
    p.SetMaxLength(2000);
}
9
1/2/2017 6:33:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow