A simpler way to use an existing database table in an existing EF Code First Blazor app.

The previous Blog and sample GitHub project provided a mechanism for a Code-First Entity Framework Blazor app to use an existing database table, which would normally be facilitated using EF Database-First. That involved creating a console app that generated the table’s class using a scaffolding command. The class was then added to the target Blazor app, add-migration run there to generate a new table in the same database, then the console app was used to copy the original table data to the new one. This did work.

BUT there is a simpler way:

  • The Records table was generated in the database in SSMS by importing a CSV file.
  • The scaffolding command is in run the Console app. (See previous Blog)
    • This generates a class for every table in the database and suggests DBSets for the DBContext.
  • The generated Record class was copied to the Blazor app.
  • The Records table in the database is temporarily renamed Recordsz.
  • A DbSet Records for Record was added to the Blazor app’s DBContext.
  • The add-migration and update-database commands were run in the Blazor context.
  • Records table generated in this last step is then deleted in the database.
  • The Recordz table is then renamed back to Records
  • The MasterRecordsClaims data was then available in the Blazor app.

SIMPLE


Further

A GitHub project for a Console app that uses a database table from slightly unstructured data consisting of Athletics event results ScanAVResults used the latter steps from the process above.

  • The class was MasterRecordClaim defined in the app to model the results data, and the app was run to populate the table
  • The table was then used in the Blazor app:
    • The MasterRecordClaims table in the database is temporarily renamed MasterRecordsClaimz.
    • A DbSet MasterRecordClaims for MasterRecordClaim was added to the Blazor app’s DBContext.
    • The add-migration and update-database commands were run in the Blazor context.
    • MasterRecordClaims table generated in this last step is then deleted in the database.
    • The MasterRecordClaimz table is then renamed back to MasterRecordClaims
    • The MasterRecordsClaims data was then available in the Blazor app.



 TopicSubtopic
   
 This Category Links 
Category:Blazor Index:Blazor
<  Prev:   Blazor Helpers Server App