Entity framework EF核心的索引注释
我在EF 6.2中有这个代码Entity framework EF核心的索引注释,entity-framework,.net-core,ef-code-first,entity-framework-core,ef-core-2.1,Entity Framework,.net Core,Ef Code First,Entity Framework Core,Ef Core 2.1,我在EF 6.2中有这个代码 public class ClientNotificationMap : EntityTypeConfiguration<ClientNotification> { public ClientNotificationMap() { HasKey(x => x.RelationalId); Property(x => x.SubscriptionId) .IsRequir
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中,它似乎不受支持。我也没有找到任何与网络相关的东西。最后我编写了代码
builder.HasIndex(x => x.SubscriptionId)
.IsUnique();
builder.Property(x => x.SubscriptionId)
.IsRequired()
.HasMaxLength(400);
它至少编译了它应该是这样的:
builder.HasIndex(x => new { x.SubscriptionId})
.HasDatabaseName("IX_SubscriptionId")
.HasFilter(null)
.IsUnique(true);
所述的
HasIndex
方法是否解决了问题?它还支持唯一和多列索引。我发现了这一点,它没有说明如何处理HasColumnAnnotation函数。如何处理它?只需将其替换为HasIndex
,这些列注释是EF6特有的解决方法,因为缺少用于定义索引的流畅API。
builder.HasIndex(x => x.SubscriptionId)
.IsUnique();
builder.Property(x => x.SubscriptionId)
.IsRequired()
.HasMaxLength(400);
builder.HasIndex(x => new { x.SubscriptionId})
.HasDatabaseName("IX_SubscriptionId")
.HasFilter(null)
.IsUnique(true);