Shortcut in the Fluent API to set multiple properties on demand

  • Here is what I am doing now:

    modelBuilder.Entity (). Property (e => e.Name) .IsRequired ();
    modelBuilder.Entity (). Property (e => e.UPC) .IsRequired ();
    modelBuilder.Entity (). Property (e => e.Price) .IsRequired ();
    modelBuilder.Entity (). Property (e => e.Description) .IsRequired ();

  • Here is what I would like to do:

    modelBuilder.Entity () .Property (e => e.Name) .IsRequired () .Property (e => e.UPC) .IsRequired () .Property (e => e.Price) .IsRequired () .Property ( e => e.Description) .IsRequired ()

The latter, however, does not work. Is there any other way not to repeat modelBuilder.Entity () every time?

  • Here is the most relevant option:

    var e = modelBuilder.Entity (); e.Property (e => e.Name) .IsRequired ();
    e.Property (e => e.UPC) .IsRequired ();
    e.Property (e => e.Price) .IsRequired ();
    e.Property (e => e.Description) .IsRequired ();

+4
source share
3 answers

This is compatible with all existing DbModelBuilder extension methods, as it simply adds a free layer on top, but it does have some syntactic overhead. Not quite what you requested, but does not include flushing with support code. Not tested yet, but it should work if you like the syntax:

// First option - like this better because it has less cruft than multiple Has invocations var modelBuilder = new DbModelBuilder(); var modelConfiguration = new ModelConfigurator(modelBuilder); modelConfiguration.Entity<Product>().Has(e => { e.Property(en => en.Name).IsRequired(); e.Property(en => en.UPC).IsRequired(); e.Property(en => en.Price).IsRequired(); e.Property(en => en.Description).IsRequired();} ); 

OR

 var modelBuilder = new DbModelBuilder(); var modelConfiguration = new ModelConfigurator(modelBuilder); modelConfiguration.Entity<Product>().Has(e => e.Property(en => en.Name).IsRequired()) .Has(e => e.Property(en => en.UPC).IsRequired()) .Has(e => e.Property(en => en.Price).IsRequired()) .Has(e => e.Property(en => en.Description).IsRequired()); // continue configuring properties, and creating methods on ModelConfigurator as needed 

Helper Code:

  public class Product{ public string Name {get;set;} public double Price {get;set;} public string UPC {get;set;} public string Description {get;set;} } public class ModelConfigurator{ public DbModelBuilder ModelBuilder{get;set;} public ModelConfigurator(DbModelBuilder modelBuilder){ ModelBuilder = modelBuilder; } public EntityConfigurator<TEntity> Entity<TEntity>() where TEntity : class { var entity = ModelBuilder.Entity<TEntity>(); return new EntityConfigurator<TEntity>(entity); } } public class EntityConfigurator<TEntity> where TEntity : class{ public EntityTypeConfiguration<TEntity> EntityTypeConfiguration {get;set;} public EntityConfigurator(EntityTypeConfiguration<TEntity> entityTypeConfiguration){ EntityTypeConfiguration = entityTypeConfiguration; } public EntityConfigurator<TEntity> Has(Action<EntityTypeConfiguration<TEntity>> a){ a(this.EntityTypeConfiguration); return this; } } 
+5
source

Another option, without the real need for Has () over Entity ():

 modelConfiguration.Entity<Product>(e => { e.Property(en => en.Name).IsRequired(); e.Property(en => en.UPC).IsRequired(); e.Property(en => en.Price).IsRequired(); e.Property(en => en.Description).IsRequired();} ); 

Extension Method:

 public static EntityTypeConfiguration<TEntity> Entity<TEntity>(this DbModelBuilder modelBuilder, Action<EntityTypeConfiguration<TEntity>> action) where TEntity : class { var r = modelBuilder.Entity<TEntity>(); action(r); return r; } 
+1
source

I think you can do the following, although I think this is rather inconvenient.

 public static class EntityConfigExtensions { public static EntityTypeConfiguration<TEntity> Prop<TEntity, TProp>(this EntityTypeConfiguration<TEntity> self, Expression<Func<TEntity, TProp>> propExpression) where TEntity : class { self.Property(propExpression); return self; } public static EntityTypeConfiguration<TEntity> RequiredProp<TEntity, TProp>(this EntityTypeConfiguration<TEntity> self, Expression<Func<TEntity, TProp>> propExpression) where TEntity : class { self.Property(propExpression).IsRequired(); return self; } // etcetera for other frequently used configs // ... // And, borrowing from David: a catch-all for the rest public static EntityTypeConfiguration<TEntity> Configure<TEntity, TProp>(this EntityTypeConfiguration<TEntity> self, Action<EntityTypeConfiguration<TEntity>> configAction) where TEntity : class { configAction(self); return self; } } 

Using:

 modelBuilder.Entity<Product>() .Prop(e => e.Name) .RequiredProp(e => e.UPC) .RequiredProp(e => e.Price) .Configure(x => x.Ignore(e => e.Description)); 
0
source

Source: https://habr.com/ru/post/1498784/


All Articles