更改所有字符串属性的最大长度 [英] change all string property max length

查看:61
本文介绍了更改所有字符串属性的最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF 6中,我可以执行以下操作:

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

因为EF7 ModelBuilder没有 Properties()函数,如何在EF7中做同样的事情?

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

推荐答案

我认为这是仍然缺乏之一,并希望它会在以后的版本中添加。

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

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

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

这篇关于更改所有字符串属性的最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆