将modelBuilder配置为一次组合多个属性配置

时间:2016-07-20 16:43:40

标签: c# entity-framework

有没有办法可以为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();

    }
}

2 个答案:

答案 0 :(得分:1)

当然可以,你只需要自己编写代码。例如,我写了一个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并更新填充以循环遍历params:

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

答案 1 :(得分:0)

目前还不可能。你必须为每个属性写那个。在max,您可以强制实体框架将.Net数据类型映射到特定的MS SQL数据类型,如here所示。