entity framework 代码中所有属性的必需规则优先
Required rule for all properties in entity framework code first
我在 Entity Framework Core 中使用流畅的 API 代码来确定属性的行为。
请问有没有办法替换这部分
modelBuilder.Entity<Person>()
.Property(b => b.Name)
.IsRequired();
modelBuilder.Entity<Person>()
.Property(b => b.LastName)
.IsRequired();
modelBuilder.Entity<Person>()
.Property(b => b.Age)
.IsRequired();
像这样:
modelBuilder.Entity<Person>()
.AllProperties()
.IsRequired();
关键是有时候大多数属性甚至全部都需要为NOT NULL。而且标记每个 属性.
也不优雅
一个解决方案可能正在使用反射:
var properties = typeof(Class).GetProperties();
foreach (var prop in properties)
{
modelBuilder
.Entity<Class>()
.Property(prop.PropertyType, prop.Name)
.IsRequired();
}
请注意,所有属性都将根据需要进行设置。当然你可以根据类型过滤需要设置的属性(例如)。
更新
使用扩展方法可以使它更干净。
EntityTypeBuilderExtensions.cs
public static class EntityTypeBuilderExtensions
{
public static List<PropertyBuilder> AllProperties<T>(this EntityTypeBuilder<T> builder,
Func<PropertyInfo, bool> filter = null) where T : class
{
var properties = typeof(T)
.GetProperties()
.AsEnumerable();
if (filter != null)
{
properties = properties
.Where(filter);
}
return properties
.Select(x => builder.Property(x.PropertyType, x.Name))
.ToList();
}
}
在您的 DbContext
中的用法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Class>()
.AllProperties()
.ForEach(x => x.IsRequired());
}
如果您只想将 IsRequired
应用于 class 的特定属性,您可以将过滤器函数传递给 AllProperties
方法。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Required is applied only for string properties
modelBuilder
.Entity<Class>()
.AllProperties(x => x.PropertyType == typeof(string))
.ForEach(x => x.IsRequired());
}
我在 Entity Framework Core 中使用流畅的 API 代码来确定属性的行为。
请问有没有办法替换这部分
modelBuilder.Entity<Person>()
.Property(b => b.Name)
.IsRequired();
modelBuilder.Entity<Person>()
.Property(b => b.LastName)
.IsRequired();
modelBuilder.Entity<Person>()
.Property(b => b.Age)
.IsRequired();
像这样:
modelBuilder.Entity<Person>()
.AllProperties()
.IsRequired();
关键是有时候大多数属性甚至全部都需要为NOT NULL。而且标记每个 属性.
也不优雅一个解决方案可能正在使用反射:
var properties = typeof(Class).GetProperties();
foreach (var prop in properties)
{
modelBuilder
.Entity<Class>()
.Property(prop.PropertyType, prop.Name)
.IsRequired();
}
请注意,所有属性都将根据需要进行设置。当然你可以根据类型过滤需要设置的属性(例如)。
更新
使用扩展方法可以使它更干净。
EntityTypeBuilderExtensions.cs
public static class EntityTypeBuilderExtensions
{
public static List<PropertyBuilder> AllProperties<T>(this EntityTypeBuilder<T> builder,
Func<PropertyInfo, bool> filter = null) where T : class
{
var properties = typeof(T)
.GetProperties()
.AsEnumerable();
if (filter != null)
{
properties = properties
.Where(filter);
}
return properties
.Select(x => builder.Property(x.PropertyType, x.Name))
.ToList();
}
}
在您的 DbContext
中的用法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Class>()
.AllProperties()
.ForEach(x => x.IsRequired());
}
如果您只想将 IsRequired
应用于 class 的特定属性,您可以将过滤器函数传递给 AllProperties
方法。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Required is applied only for string properties
modelBuilder
.Entity<Class>()
.AllProperties(x => x.PropertyType == typeof(string))
.ForEach(x => x.IsRequired());
}