Foreign key conflict ef core scaffolding

c# entity-framework-core

Question

The foreign key {'ClassExtId'} cannot be added to the entity type 'ClassInt' because a foreign key on the same properties already exists on entity type 'Instrument' and also targets the key {'ClassExtId'} on 'ClassExt'.

Let's say I have ClassFoo & ClassBar inheriting from ClassInt. Each one of them have a reference to the same column ClassExtId. Why is the entity framework core scaffolding (v3) complaining ?

1
3
10/15/2019 9:59:03 AM

Popular Answer

my case is different but i got the same error too when i was trying to scaffold my existing old database. i'm, posting this answer just in case someone also had the same problem with me.

it's turn out that in my old database, some table have so many duplicate foreign key created by previous developer. you can check this in your sql server (i use sql server developer edition 2019)

  1. In Object Explorer, right-click the table that will be on the foreign-key side of the relationship and click Design.
    The table opens in Table Designer.

  2. From the Table Designer menu, click Relationships. you'll see all the foreign key defined for that table, in my case it was applicationID that duplicate. clean it up : enter image description here

  3. go to your table > expand it and go to Keys. delete the duplicate key there: enter image description here

  4. run your dotnet ef dbcontext scaffold command and good luck!
1
1/28/2020 4:02:18 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow