IndexAnnotation到EF Core

3

我在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中似乎不支持此功能。同时在网页相关内容中也没有找到任何相关信息。

1
这里描述的 HasIndex 方法是否解决了问题?它还支持唯一和多列索引。 - dandev486
我找到了这个,但它没有说明如何处理HasColumnAnnotation函数。 - user3417479
1
如何处理呢?只需用 HasIndex 替换它,这些列注释是 EF6 特定的解决方法,用于定义索引缺乏流畅 API。 - Ivan Stoev
2个回答

4

最后,我编写了代码。

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

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

至少编译通过了


1

应该是这样的:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接