Issue with DbContext reading in DB Tables
I am trying to use DbContext and the optionsBuilder to connect to my database and obtain a table called "Card". I am doing the following command in :
and also have the following object instantiated in the same class:
Then, in my main method I run the following lines:
I get an error that "Card" is not a known table even though I have a Card table in the database. What am I doing wrong?
71 Replies
you should be doing 1 of 2 things
1. create your dbcontext then use that to generate migrations to create your database
2. scaffold your dbcontext from your existing database
otherwise you get issues like this where things aren't named what you expect
how do you generate migrations?
right now my card table resembles my card class
Migrations Overview - EF Core
Overview of using migrations to manage database schemas with Entity Framework Core
Ah yeah, i've tried running but it gives me this error:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET program, but dotnet-ef does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Did you install th dotnet-ef tool?
ah maybe not
that fixed it
afterall, if you have just testing DB and no important data, you can use EnsureDeleted then EnsureCreated)
So now that I have the intiialcreate ran in the terminal do i have to manually add the tables with ?
No
DbContext
represents the database
DbSet<T>
s within represent the tables
Each T
represents the shape of the tableI see. so is there still a chance that it's not named card as i expected? it still doesnt seem to work
This will create a database with
Users
and Blogposts
tables
Blogpost having a foreign key to User
Both having an auto-incremented integer primary keyYeah
this is part of my card class, there are some methods as well but i didnt include them here
And this things that are mapped are the same name in the database as well
[NotMapped]
💀
Get into the habit of using DTOs
Database models should only describe the database tableGotcha. I wasnt sure how else to represent this class because it requries the values that arent mapped and i wouldnt wanna represent those values in the database
but with that aside, is there anythign else im missing? it doesnt seem like the migrations fixed it
Show your code and errors