3

Is there a way that I can combine more than one property for Required, MaxLength and HasColumn or do I need to create one for each property?

I would like to be able to include multiple fields to be required along with assisgning them the same MaxLength if they are instead of creating a new one for each field in the entity like I'm doing below now.

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

    }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Brian
  • 131
  • 1
  • 7
  • 16
  • use string varchar50 = "varchar(50)"; and use from modelBuilder.Entity() .Property(d => d.DeptName) .HasColumnType(varchar50 ) .HasMaxLength(50) .IsRequired(); – Mohammad Akbari Jul 20 '16 at 17:15
  • 2
    Such attempts tend to make code less flexible than it should be. There's one inconsistency, a required field with max length 10 (not 50). How would you deal with that? – Gert Arnold Jul 20 '16 at 17:33
  • Thanks for replying back, but I would like to know how if possible can I include multiple fields in a single entity by only calling it once. Perhaps I'm not explaining it properly. Please see what I would like to know is possible or not. `modelBuilder.Entity() .Property(e => e.EmpFirstName), (e => e.EmpLastName) .HasColumnType("varchar(50)") .IsRequired();` – Brian Jul 20 '16 at 18:10

2 Answers2

1

Sure you can, you just have to write the code yourself. For example, I wrote a EntityTypeConfigurationExtensions that allows you to configure a entity with multiple properties in a single call instead of multiple calls. I don't see why you couldn't modify my code to use params and then you could pass multiple properties:

(you'd have to make the propertyConfiguration come first, then the 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;
    }
}

Now this:

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

Is now:

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

So modiying my code to use params and updating the plumping to just loop through the params would yeild:

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,);
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

No that's not possible as of now. You have to write that for each property. At max you can coerce Entity Framework to map a .Net data type to a particular MS SQL data-type as shown here.

Community
  • 1
  • 1
Ankit
  • 2,448
  • 3
  • 20
  • 33