实体类型配置和属性配置的扩展方法<datatype>



使用实体框架,我可能有一个配置,看起来像:

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

经过一段时间的尝试后,我意识到我不需要执行/编译其中的任何内容,我只需要按照正确的顺序将参数链接在一起。

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

相关内容

最新更新