✅ Should we include database name in connection string when creating new db using EF core ?
Hello guys, I have an existing connection string with an existing database name "Library". I wanted to create a new database Students. I used the existing connection string and in that, their was the key Database with value Library. What happens is that I didn't get a database of Students being generated but rather, I get Students table inside Library with the migration file in the Library database. Is this normal? Should I omit the key database in the connection string? Or I just change database name in connection string to Students?
15 Replies
In my connection string, I just change Library to Students, it seems that my db Library got overwritten, is that normal?
This will probably depend on which database provider you end up using. I normally use the same database with postgres but specify different schemas for each of my projects
if these projects aren't related i would, at the very least, try to have them use different schemas
ah, so you use an existing database, just create different schemas?
that is what i normally do, it's similar to how we handle our databases at work as well
yep I see, I need to investigate, don't know why my library db no longer exists when I switch the database name in my connection string, I thought it will just create a new one
unless there is a specific reason you want to use multiple databses, don't
even different schemas is probably unnecessary if it's all the same application
Hmm the thing is in our connection string we use the name of the database that we want to create or the name of any existing db where the tables will be created
i'm confused exactly what you're trying to accomplish
"library" and "students" sound like tables, not databases
The thing is I had a db library which contain as tables books, members etc... for the students yeah probably should have been a better name. Initially I had my Library db. When I add and update the migration file, I was expecting to have another db, students.
At first in my connection string, I use database=Library. What happen is that no new db was created instead I got a new table inside my db Library.
What I did next is, I change my connection string db to the db name I want to create, Students (yeah not a good db name was just for testing). What happened when I add and update migration file is, the students db was created but library db was kind of deleted.
an EF dbcontext is just that, a database context
it only represents one database
EF will not delete a random database as part of a migration so i don't know what you did there
Yeah I need to have a look once more 😭 nvm it was just a test db, will have a look tomorrow then come back, Im really tired right now :c
I try the same thing, it seems to work normally now
Don't know why I get this unexpected behaviour yesterday
Just to clarify, say I want to create a database
School
, in the connection string, we need to specify the database we want to use, there also we specify School
even though it don't exist yet?
anyone can just confirm pleaseYes, and if your using EF it will either create that db if not exists and apply the migrations or if exists use it.
yep I see, thanks !
With out seeing your EF setup it is hard to say exactly what you have going on with setup and tear down(if any) of your db.
yeah but should be good for now on my side, now I will try to do some modifications to see how all of this work, thanks !