更改所有字符串属性最大长度

cap*_*ono 7 c# entity-framework entity-framework-core

在EF 6中,我可以这样做:

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

由于EF7 ModelBuilder没有这个Properties()功能,我如何在EF7中做同样的事情?

Iva*_*oev 9

我认为这是EF Core中"仍然缺乏"的功能之一,并期望在以后的版本中添加它.

在那之前,我能建议的最接近(对于v1.1.0)如下:

foreach (var p in modelBuilder.Model
    .GetEntityTypes()
    .SelectMany(t => t.GetProperties())
    .Where(p => p.ClrType == typeof(string) && p.GetMaxLength() == null))
{
    p.SetMaxLength(2000);
}
Run Code Online (Sandbox Code Playgroud)