question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrating to 2.1 adds extra indexes and foreign keys

See original GitHub issue

I have the following data structure:

public class Foo {
    public long Id;
    public virtual ICollection<Bar> Bars { get; set; }
}

public abstract class Bar {
    // properties
}

public class Bar1 : Bar {
    public long FooId { get; set; }
    public virtual Foo Foo { get; set; }
}

public class Bar2 : Bar {
    public long FooId { get; set; }
    public long Foo Foo { get; set; }
}

// these two lines are inside the OnModelCreating method in the context
modelBuilder.Entity<Bar1>.Property(x => x.FooId).HasColumnName("FooId");
modelBuilder.Entity<Bar2>.Property(x => x.FooId).HasColumnName("FooId");

The initial migration for the code was generated using 2.0 and the Bar table had only one index and one foreign key. After making changes to the model, the first migration with 2.1 added another foreign key and index to the same property.

Is this normal, because I couldn’t find anything about this change.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Jun 4, 2018

@dushkostanoeski You need to move the navigation Foo to the base - Bar to indicate that both derived relationships should be represented by a single FK. Otherwise you can just configure the FK name on both relationships explicitly to the same value.

0reactions
dushkostanoeskicommented, Jun 4, 2018

@AndriySvyryd I just upgraded to the 2.1.0 release, and the migration still added the extra indexes and foreign keys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to stop EF Core from indexing all foreign keys
As documented in questions like Entity Framework Indexing ALL foreign key columns, EF Core seems to automatically generate an index for every ...
Read more >
Migration: Changing an index that is part of an FK ...
Changed some indexes from being unique to non-unique, but since they all are in foreign key constraints they won't update since the FK's ......
Read more >
Active Record Migrations
This migration adds a table called products with a string column called name and ... References are a shorthand for creating columns, indexes,...
Read more >
Loose foreign keys
When the loose foreign key definition is no longer needed (parent table is removed, or FK is restored), we need to remove the...
Read more >
Using a PostgreSQL database as an AWS DMS source
You can migrate data from one or many PostgreSQL databases using AWS DMS. ... with the same name and recreates the tables, indexes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found