Answered by:
Blazor - Scaffold-DBContext issues?

Question
-
User379720387 posted
I am using this tutorial to educate myself on Blazor
Scaffold-DBContext seems pretty straightforward to set up:
First, we will install the package for the database provider that we are targeting which is SQL Server in this case. Hence, run the following command:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Since we are using Entity Framework Tools to create a model from the existing database, we will install the tools package as well. Hence, run the following command:
Install-Package Microsoft.EntityFrameworkCore.Tools
After you have installed both the packages, we will scaffold our model from the database tables using the following command:
Scaffold-DbContext "Your connection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Employee, Cities
Do not forget to put your own connection string (inside
" "
). After this command gets executed successfully you can observe a Models folder has been created inside ServerSideSPA.App project and it contains three class files myTestDBContext.cs, Cities.cs, and Employee.cs. Hence, we have successfully scaffolded our Models using EF core database first approach.I did get the Models folder and the 3 class files, but I don't think I actually have a connection to the db, so I am wondering about "your connection string here"
Then I found the learnentityframeworkcore.com walkthrough for an existing database here
If you are working with Visual Studio, you can use the Package Manager Console commands to generate the the code files for the model. The equivalent command to the last CLI command just above is:
PM> Scaffold-DbContext "Server=.\;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context "AdventureContext" -DataAnnotations
I took that as (Package Manager console):
"Server=S4000W\SQLSERVER;Database=BLZR;Trusted_Connection=True;"
Now I am wondering if the Models folder and classes got created without actually connecting to my db?
Friday, November 23, 2018 1:09 PM
Answers
-
User-821857111 posted
My question is this.... can I be sure that when the scaffolding succeeds and creates the model class that I have access to the db?
Don't know enough to test for myself that there is a connection to the db.
You can use SQL Server Profiler to see the commands being executed against your database: https://docs.microsoft.com/en-us/sql/tools/sql-server-profiler/start-sql-server-profiler?view=sql-server-2017
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 26, 2018 4:30 PM
All replies
-
User1120430333 posted
Why don't you just delete the whole project and start over, a simple approach?
Friday, November 23, 2018 1:22 PM -
User379720387 posted
yes, that is the plan.
Right now I don't have any other information than what I tried first. My expectation would be the same outcome.
Any thoughts on the connection string?
Friday, November 23, 2018 2:33 PM -
User-821857111 posted
You need to specify a connection string for the tooling to use so that it can examine the database schema and generate the appropriate model files. So I don;t know how your first attempt worked unless you supplied a connection string.
The connection string is a standard database connection string. What you posted looks valid.
Friday, November 23, 2018 3:05 PM -
User379720387 posted
I did use this:
Scaffold-DbContext "Server=S4000W\SQLSERVER;Database=BLZR;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Employee, Cities
It created the DBContext, but I don't think there was an actual connection to the db.
Friday, November 23, 2018 7:06 PM -
User-821857111 posted
Why don't think a connection was made to the database?
Friday, November 23, 2018 8:09 PM -
User379720387 posted
It wasn't loading the data that is in the db.
Saturday, November 24, 2018 2:24 AM -
User-821857111 posted
The scaffolding process doesn't query data. The data itself has no value in the process. It queries the database schema. It constructs the model files from the schema.
Sunday, November 25, 2018 7:40 PM -
User379720387 posted
I realize that.
The three model class files were created
All the code is there to show the Employee records. Nothing came up, that is why I am considering if model files can be created without having a connection to the db.
Sunday, November 25, 2018 10:07 PM -
User-1764593085 posted
Hi wavemaster,
If you have correct Server name, there seems no problem with your connection string.You could refer to EF core Db first tutorial to do that with localdb.
Make sure seed data to your Employee table in database. Then you could also check your code to the source code of your blazor tutorial to find the differences.
Xing
Monday, November 26, 2018 2:31 AM -
User-821857111 posted
wavemaster
All the code is there to show the Employee records. Nothing came upProbably my bad, but I'm struggling to follow. Where is all the code to show the Employee record? Where do you expect the data to "come up"? I'm unclear where in the process you expected data to appear and why you expect it.
Monday, November 26, 2018 11:16 AM -
User379720387 posted
I am following this tutorial here:
https://dzone.com/articles/single-page-application-using-server-side-blazor-a
Towards the bottom "EXECUTION DEMO" and the fetchemployee part.
I added an Employee record directly in the db.
Should have come up on the fetchemployee page.
Monday, November 26, 2018 11:55 AM -
User-821857111 posted
I'm unclear what that has to do with scaffolding.
Monday, November 26, 2018 1:00 PM -
User379720387 posted
My question is this.... can I be sure that when the scaffolding succeeds and creates the model class that I have access to the db?
Don't know enough to test for myself that there is a connection to the db.
Monday, November 26, 2018 2:10 PM -
User475983607 posted
My question is this.... can I be sure that when the scaffolding succeeds and creates the model class that I have access to the db?The command had to open and connect to the database to read the table schemas and create the DbContext and Entities as explained above.
Maybe, the connection used to scaffold the DB is not the same DB used to add records.
Monday, November 26, 2018 3:17 PM -
User-821857111 posted
My question is this.... can I be sure that when the scaffolding succeeds and creates the model class that I have access to the db?
Don't know enough to test for myself that there is a connection to the db.
You can use SQL Server Profiler to see the commands being executed against your database: https://docs.microsoft.com/en-us/sql/tools/sql-server-profiler/start-sql-server-profiler?view=sql-server-2017
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 26, 2018 4:30 PM