EntityTypeConfiguration和<datatype> PropertyConfiguration

时间:2016-10-12 18:03:08

标签: c# entity-framework

使用Entity Framework,我的配置可能如下:

internal class MyDbContext : DbContext
{

  ....

  protected override void OnModelCreating(DbModelBuilder mb)
  {
    builder.Entity<MyEntity>()
      .ToTable("MyTable", "MySchema");

    builder.Entity<MyEntity>()
      .Property(e => e.Name)
      .IsRequired()
      .HaxMaxLength(10);

    builder.Entity<MyEntity>()
      .Property(e => e.City)
      .HaxMaxLength(10);

  }
}

我想写一个扩展方法,所以我可以这样写:

    builder.Entity<MyEntity>()
      .ToTable("MyTable", "MySchema")
      .Property(e => e.Name, 
        n => n.IsRequired()
              .HaxMaxLength(10))
      .Property(e => e.City,
        c => c.HasxMaxLength(50));

我非常确定我的签名是正确的,但我不知道如何让内部管道正常工作。

    public static EntityTypeConfiguration<TEntityType> Property<TEntityType>(
        this EntityTypeConfiguration<TEntityType> instance,
        Expression<Func<TEntityType, byte[]>> propertyExpression, 
        Func<BinaryPropertyConfiguration, BinaryPropertyConfiguration> propertyConfiguration)
        where TEntityType : class
    {
        Func<TEntityType, byte[]> func = propertyExpression.Compile();

        // ??

        return instance;
    }

1 个答案:

答案 0 :(得分:0)

经过一番游戏,我意识到我不需要执行/编译任何一个,我只需要按正确的顺序将参数链接在一起。

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