繁体   English   中英

EntityTypeConfiguration 的扩展方法和<datatype>属性配置

[英]Extension methods for EntityTypeConfiguration and <datatype>PropertyConfiguration

使用实体框架,我可能有一个如下所示的配置:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM