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
thats all the relevant code i beleive
And what's the error?
What do migrations say?
No, the cli when you migrate
And when you apply that migration
huh
those are the only two commands i have done
Huh
D:
any other suggestions to try? i feel like its a really silly mistake but im not sure what im overlooking
Delete
mtg.db
file, delete the migrations, create the migration and apply it again
Only thing that comes to minddo i delete the .dll and exe of it too?
there ar a few diufferent file types of the same name
What dll and exe...?
Where is that?
net8.0 folder
is there another place to delete it
Right... you know what, just delete the
bin
and obj
folders altogether
It's safe to do, no worrieskk
and now do
dotnet ef migrations remove
?Nah, just remove the
Migrations
folderokie
and re run ?
or just run those two migration commands
and then re run program
yeah
got a diff output when i did update now hm
Oh?
but the program still doesnt work ðŸ˜
Huh
I... think I know what might be happening here
ill be back on later but if you guys have any suggestiuons that would be helpful
applying migrations to the wrong db? :LUL:
It's creating a new database file with each build
the build itself shouldn't, but running the migrations would create one wherever the connection string says to
I wonder if creating
mtg.db
in the project root and copying it on build would fix itwdym copying it on build?
i access the db in ssms
ssms works with sqlite?
are you 100% sure that the file you're applying migrations to and the one your program is opening are the same?
See, that's why I prefer running Pg even for small projects like this, no need to worry about which file I open, which gets used, which gets created, and whether all three of those are the same file or not :KEKW:
sqlite also has some quirks with write-ahead logging that can store data in a file other than the main .db file and cause more confusion
i am not sure ...😂
do yall know what companies with big databases in ssms usually use to connect to c# if not dbcontext?
that's not the problem here, EF core is a very common and well supported library
ssms isn't a database, it's a tool to manage databases
in your case your actual database is sqlite
ah i see what u mean
Well i created the database in ssms. does it make it sqlite by default?
i have no idea, ssms is primarily used to manage MS SQL Server instances
you shouldn't be creating the database in ssms at all if you're using migrations
oof
it will be created when you apply the migrations
so how would i instead connect it to the one made in ssms
and since sqlite works with files and not a connection to a server, you have to make sure the file you're migrating and the file your program is using at runtime are the same file
or how would i make a new one that's still accessible in ssms i suppose
gotcha
How would I verify that?
check the working directory of your program and make sure that the db file in that folder is the one you're migrating or opening in ssms
afaik sqlite also does not like multiple programs opening the same db at once, so i'd avoid doing that
how would ik what i'm migrating tho ?
it uses the same connection string that you put in
OnConfiguring
ah yeah
and that file will be relative to the working directory of the program, which is probably the folder where the exe is but it can also be the project folder
i'm pretty sure it's not the same as the one in sssms since i created the db in sssms
but i'm not sure how to point it to that one
honestly unless you need sqlite specifically i'd skip the headache and install a proper database server like sql express
sqlite databases are just files, you'd point it by opening the database that you want to use
gotcha, will look into that when i get home
so do i have to re create the database using sql express? or how would this work
you'll need to recreate the migrations for sql server instead of sqlite, that's about it
then use a connection string for your sql express instance instead of a sqlite database file
how do you point the migrations at the sql server tho ? do u just change the cmd in the cli
by changing the ef core provider library and connection string
so instead of
.UseSqlite(...)
it would be .UseSqlServer(...)
ohhh gotcha
I blame chat gpt lmao
I'll try that out soon and lyk how it goes
alright well im getting a diff error now at least:
on ssms im connecting through localhost tho, i wasnt able to get my other server to work
im using this instead but now i get this issue:
I think I have it working now (ik its bad practice but i ended up adding "Encrypt=False"). Thanks so much for your help guys !