1

I have tried to scaffold two views from a database in a SQL Server. Code in .Net 5.0.

Scaffold-DbContext "conn-string" 
     Microsoft.EntityFrameworkCore.SqlServer
     -OutputDir Entities -ContextDir . 
     -Context MyContext -UseDatabaseNames -Force 
     -NoPluralize -NoOnConfiguring -Tables View1,View2

This runs without error but no entities for this views are generated and I get a message:

Unable to find a table in the database matching the selected table 'View1'.

Unable to find a table in the database matching the selected table 'View2'.

How do I use Scaffold-DbContext to get these two views?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Aside from referencing the db schema, you could be stuck with this error because the actual project itself does not compile.

I commented out all the code associated to the DbContext model, and ran the following equivalent Scaffold command.

Scaffold-DbContext "Server=MyServer;Database=myDb;user=theUser;password=thePwd;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities -ContextDir . -Context MyContext -UseDatabaseNames -Force -NoPluralize -NoOnConfiguring -Tables <<your database view>>

The command ran successfully and created my class in the root folder. I then moved it into my Models folder and went from there. That is optional of course.

There is more info here as to why you need to make sure the solution compiles before you run the scaffold command.

Fandango68
  • 4,461
  • 4
  • 39
  • 74