change all string property max length

巧了我就是萌 提交于 2019-12-18 05:59:52

问题


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:


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);
}


来源:https://stackoverflow.com/questions/41427030/change-all-string-property-max-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!