以下是我目前正在做的事情:
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();
我想做的是:
ModelBuilder.Entity()
.property(e=>e.name).isRequired()
.property(e=>e.upc).isRequired()
.property(e=>e.price).isRequired()
.property(e=>e.description).isRequired()
不过,后者不起作用。是否有其他方法不必每次都重复modelbuilder.entity()?
以下是目前最简单的选择:
var e=modelbuilder.entity();
e.property(e=>e.name).isRequired();
e.属性(e=>e.upc).isRequired();
e.property(e=>e.price).isRequired();
e.property(e=>e.description).isRequired();

最佳答案

这与所有现有的DbMultuBu建德扩展方法兼容,因为它只是在上面添加了一个FLUENT层,但是它确实带有一些语法开销。不完全是你所要求的,但不涉及与支持代码混在一起。还没有完全测试过,但是如果您熟悉语法,它应该可以工作:

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

或者
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

支持代码:
  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;
        }
    }

10-04 12:36