C
C#2d ago
Faker

✅ 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
Faker
FakerOP2d ago
In my connection string, I just change Library to Students, it seems that my db Library got overwritten, is that normal?
Keswiik
Keswiik2d ago
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
Faker
FakerOP2d ago
ah, so you use an existing database, just create different schemas?
Keswiik
Keswiik2d ago
that is what i normally do, it's similar to how we handle our databases at work as well
Faker
FakerOP2d ago
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
Jimmacle
Jimmacle2d ago
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
Faker
FakerOP2d ago
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
Jimmacle
Jimmacle2d ago
i'm confused exactly what you're trying to accomplish "library" and "students" sound like tables, not databases
Faker
FakerOP2d ago
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.
Jimmacle
Jimmacle2d ago
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
Faker
FakerOP21h ago
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 please
glhays
glhays20h ago
Yes, and if your using EF it will either create that db if not exists and apply the migrations or if exists use it.
Faker
FakerOP20h ago
yep I see, thanks !
glhays
glhays20h ago
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.
Faker
FakerOP20h ago
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 !

Did you find this page helpful?