簡體   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