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?

2
  • 1
    Add the schema to the name: dbo.View1,dbo.View2 Commented Oct 5, 2021 at 9:48
  • Thanks. I had that. But you made me look again and it was a copy/paste problem. Wrong database. Commented Oct 5, 2021 at 10:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.