none
EF Core 2.1 and Visual Studio 2017 (15.9.7) with ASP.NET Core 2.1 Project: Scaffolding doesn't create all .OnDelete and navigation properties from existing SQL-Database RRS feed

  • Question

  • Hello,

    I use a MS-SQL database (migrated from Access with SQL Server Migration Assistant for Access) and I have to develop an ASP.NET Core project using this database.

    For this I am using Microsoft Visual Studio Professional 2017 (15.9.7) with ASP.NET Core 2.1 and EF Core 2.1.

    The "Scaffold-DBContext" in Visual Studio Professional doesn't create all ".OnDelete" objects in DBContext for all tables with foreign keys and therefore also the navigation properties are missing then.
    I am not able to see differences in the foreign keys at the tables where ".OnDelete" and navigation properties are created...

    Does someone have an idea how to fix this?

    Thank you very much in advance!

    Kind regards,
    Robert.

    Wednesday, June 19, 2019 9:00 AM

All replies

  • In my opinion, you should have created the database and its tables manually. You could then use the EF Core scaffolding for an existing database. You can also just point the EF Core scaffolding to the existing database you have created on MS SQL Server and rebuild the Model to see if that corrects your problem.

    https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

    Wednesday, June 19, 2019 12:16 PM
  • Hello,

    I also used this Scaffold-DbContext command mentioned in the link you posted and alread deleted all the created model files and use the command again but with no changes, for some entites the OnDelete and also Navigation propoerties are missing.

    The database contains about 20 tables with Relations/forein keys and alread contains data because it had to replace an existing access database with an ASP.NET application.

    Kind regards,
    Robert.

    Wednesday, June 19, 2019 12:24 PM
  • Have you used SSMS and looked at the table schemas to verify that the migration worked as expected? 

    Myself personally, I don't use the migration technology in  EF.

    Wednesday, June 19, 2019 6:24 PM
  • Yes, after the Migration from Access to SQL I also used SMSS and checked the tables and foreign keys and also added new columns to some tables.

    I also tried to remove the foreign key from an affecting table, added it again via SMSS but after that EF Core also didn't add the Navigation Property at scaffolding db-context and also didn't add the .ondelete property.

    Very strange...

    Friday, June 21, 2019 6:38 AM
  • Yes, after the Migration from Access to SQL I also used SMSS and checked the tables and foreign keys and also added new columns to some tables.

    I also tried to remove the foreign key from an affecting table, added it again via SMSS but after that EF Core also didn't add the Navigation Property at scaffolding db-context and also didn't add the .ondelete property.

    Very strange...


    Maybe you should use SSMS,  create some tables based on you requirements and see if the problem follows using EF Core scaffolding.  
    Friday, June 21, 2019 11:29 AM