1

.NET Core 3.1, EF Core 5.0 preview 6

Hello,

I have Foreign Key, but don't want to have Index for this property (I need to create this same Database as from "classic" EF 6).

My configuration:

entity.HasOne(d => d.Tenant)
    .WithMany()
    .HasForeignKey(d => d.TenantId)
    .OnDelete(DeleteBehavior.ClientSetNull)
    .HasConstraintName("FK_MyFKName");

But this creates:

CONSTRAINT [FK_MyFKName] FOREIGN KEY ([TenantId]) REFERENCES [dbo].[Tenant] ([TenantId])

CREATE NONCLUSTERED INDEX [IX_Contact_TenantId]
    ON [dbo].[Contact]([TenantId] ASC);
GO

How to disable creating Index? Or this was incorrect in EF Classic and I must have both FK and Index?

Saibamen
  • 610
  • 3
  • 17
  • 43
  • 1
    Have you already read this: https://stackoverflow.com/questions/49432203/how-to-stop-ef-core-from-indexing-all-foreign-keys – derstauner Aug 06 '20 at 12:08
  • 2
    I think you certainly want an index on a field like TenantId. It will be heavily filtered. If there is a legacy database without them, add them as soon as possible. – Gert Arnold Aug 06 '20 at 12:29
  • Even though your queries don`t use these indexes, they speedup the FK constraint enforcement at the first place, and cascade delete if requested. So having them is de facto a standard in RDBMS. To say it differently, the FK constraint is a rule, while indexes help implementing that rule efficiently. Same for UNIQUE constraint vs UNIQUE index. – Ivan Stoev Aug 07 '20 at 06:16

0 Answers0