2013-08-25 19 views
4
  1. 这里设置多个属性就是我目前在做:快捷方式流利的API为需要

    modelBuilder.Entity()房产(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();

  2. 这是我想在做什么:

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

后者,虽然,不工作。有没有其他的方法不必每次重复模型构建器。实体()?

  1. 这里是当前最简练选项:

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

+1

不是我所知道的。你的第一个例子是唯一的方法AFAIK –

+1

我知道使用流利的API链接有一些限制,但不能确切地记得它们是什么。看看这个文档,我看不到任何''的例子。Property'对于一个实体使用两次http://msdn.microsoft.com/en-gb/data/jj591617 – ojhawkins

+0

'resharper templates'想起来 –

回答

5

这是所有的现有DbModelBuilder扩展方法,因为它只是在上面添加流利层兼容,但它也确实会带来一些语法开销。不完全是你要求的,但不涉及支持代码。还没有完全测试此还,但如果你熟悉的语法它应该工作:

// 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 

支持代码:

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; 
     } 
    } 
+0

这很有效,所以我将它标记为答案。但是,由于它的语法开销,我可能会继续使用标准的Fluent API,因为原始问题中的第三个选项对于其他开发人员来说很容易理解,同时也很合理。 –

+0

我认为这是最有意义的,除非你想花大量的时间重写现有的流利接口,覆盖很多内部。 – DavidN

0

我想你能做到以下几点,但我觉得这是比较尴尬的。

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; 
    } 
} 

用法:

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

我收到错误消息:“'new()'约束不能与'结构'限制一起使用。“ –

+0

你说得对,需要删除。有趣的是MSDN文档状态: 公共 物业PrimitivePropertyConfiguration ( \t表达> propertyExpression ) 其中T:结构,新的() – Alex

+0

@ShaunLuttin如果MSDN文档是不正确的,你可能还需要尝试没有'TProp:struct'约束。 – Alex

1

另一种选择,没有Has()在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();} 
           ); 

扩展方法:

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; 
}