gpt4 book ai didi

c# - 配置 modelBuilder 一次组合多个属性配置

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:17 27 4
gpt4 key购买 nike

有没有一种方法可以为 Required、MaxLength 和 HasColumn 组合多个属性,或者我是否需要为每个属性创建一个?

我希望能够包括多个必填字段,并为它们分配相同的 MaxLength,而不是像我现在在下面做的那样为实体中的每个字段创建一个新字段。

public class DataEntryContext : DbContext
{
public DataEntryContext(DbContextOptions<DataEntryContext> options)
:base (options)
{ }

public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasKey(e => e.EmpId);

modelBuilder.Entity<Employee>()
.Property(e => e.EmpFirstName)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();

modelBuilder.Entity<Employee>()
.Property(e => e.EmpLastName)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();

modelBuilder.Entity<Employee>()
.Property(e => e.EmpPhoneNumber)
.HasColumnType("varchar(10)")
.HasMaxLength(10)
.IsRequired();

modelBuilder.Entity<Employee>()
.Property(e => e.EmpStartDate)
.HasColumnType("datetime")
.IsRequired();


modelBuilder.Entity<Department>()
.HasKey(d => d.DeptId);

modelBuilder.Entity<Department>()
.Property(d => d.DeptName)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();

}
}

最佳答案

当然可以,您只需要自己编写代码即可。例如,我编写了一个 EntityTypeConfigurationExtensions,它允许您在一次调用而不是多次调用中配置具有多个属性的实体。我不明白为什么您不能修改我的代码以使用 params 然后您可以传递多个属性:

(您必须先设置 propertyConfiguration,然后设置 propertyExpression)

public static class EntityTypeConfigurationExtensions
{
public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, byte[]>> propertyExpression,
Func<BinaryPropertyConfiguration, BinaryPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, Guid>> propertyExpression,
Func<PrimitivePropertyConfiguration, PrimitivePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, Guid?>> propertyExpression,
Func<PrimitivePropertyConfiguration, PrimitivePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, TimeSpan?>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, TimeSpan>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, DateTimeOffset?>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, DateTimeOffset>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, DateTime?>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, DateTime>> propertyExpression,
Func<DateTimePropertyConfiguration, DateTimePropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, decimal?>> propertyExpression,
Func<DecimalPropertyConfiguration, DecimalPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, decimal>> propertyExpression,
Func<DecimalPropertyConfiguration, DecimalPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}

public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
this EntityTypeConfiguration<TEntityType> instance,
Expression<Func<TEntityType, string>> propertyExpression,
Func<StringPropertyConfiguration, StringPropertyConfiguration> propertyConfiguration)
where TEntityType : class
{
propertyConfiguration(instance.Property(propertyExpression));

return instance;
}
}

现在这个:

modelBuilder.Entity<Employee>()
.HasKey(e => e.EmpId);

modelBuilder.Entity<Employee>()
.Property(e => e.EmpFirstName)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();

modelBuilder.Entity<Employee>()
.Property(e => e.EmpLastName)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();

modelBuilder.Entity<Employee>()
.Property(e => e.EmpStartDate)
.HasColumnType("datetime")
.IsRequired();

现在是:

modelBuilder.Entity<Employee>()
.HasKey(e => e.EmpId)
.Property(e => e.EmpFirstName,
p => p.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired())
.Property(e => e.EmpLastName,
p => p.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired())
.Property(e => e.EmpStartDate,
p => p.HasColumnType("datetime")
.IsRequired());

因此修改我的代码以使用 params 并更新 plumping 以仅循环遍历参数将产生:

modelBuilder.Entity<Employee>()
.HasKey(e => e.EmpId)
.Property(p => p.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired(),
e => e.EmpFirstName,
e => e.EmpLastName);
.Property(p => p.HasColumnType("datetime")
.IsRequired(),
e => e.EmpStartDate,);

关于c# - 配置 modelBuilder 一次组合多个属性配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com