【问题标题】:IndexAnnotation to EF CoreEF Core 的 IndexAnnotation
【发布时间】:2019-05-06 21:21:54
【问题描述】:

我在 EF 6.2 中有这段代码

public class ClientNotificationMap : EntityTypeConfiguration<ClientNotification>
{
    public ClientNotificationMap()
    {
        HasKey(x => x.RelationalId);

        Property(x => x.SubscriptionId)
            .IsRequired()
            .HasMaxLength(400)
            .HasColumnAnnotation(IndexAnnotation.AnnotationName, new  IndexAnnotation(new IndexAttribute()));
     }
}

我想迁移到 Core 2.2

public class ClientNotificationMap : IEntityTypeConfiguration<ClientNotification>
{
    public ClientNotificationMap()
    {

    }

    public void Configure(EntityTypeBuilder<ClientNotification> builder)
    {
        builder.HasKey(x => x.RelationalId);

        builder.Property(x => x.SubscriptionId)
            .IsRequired()
            .HasMaxLength(400)
            .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
    }
}

有谁知道怎么改

                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));

在 Core 中似乎不受支持。我也没有找到任何与网络相关的东西。

【问题讨论】:

  • HasIndex 描述的 here 方法是否解决了问题?它还支持唯一和多列索引。
  • 这个我找到了,没有说HasColumnAnnotation函数怎么处理。
  • 如何处理?只需将其替换为HasIndex,这些列注释是 EF6 特定的解决方法,它缺乏用于定义索引的流畅 API。

标签: entity-framework .net-core ef-code-first entity-framework-core ef-core-2.1


【解决方案1】:

最后我写了代码

        builder.HasIndex(x => x.SubscriptionId)
            .IsUnique();

        builder.Property(x => x.SubscriptionId)
            .IsRequired()
            .HasMaxLength(400);

至少可以编译

【讨论】:

    【解决方案2】:

    应该是这样的:

    builder.HasIndex(x => new { x.SubscriptionId})
    .HasDatabaseName("IX_SubscriptionId")
    .HasFilter(null)
    .IsUnique(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-29
      • 2020-12-08
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2018-12-07
      • 1970-01-01
      相关资源
      最近更新 更多