❔ EF Core not create model on start-up
So I have an ASP.NET Web API running on docker which works. So does the SQL DB . The API can connect to the database. I can open the database with SSMS. But it only contains the default system databases.
I have a working DBContext but somehow when in the docker container after performing docker-compose up, the Model has not been created. I added following line in the Program.cs
It works without docker
27 Replies
by "the model has not been created" do you mean the migrations aren't running?
seems liek it yes
you must tell EF explicitly that you want to run migrations
$migrations
To migrate on app startup
https://medium.com/@floyd.may/ef-core-app-migrate-on-startup-d046afdba258
Other migration strategies https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/applying?tabs=dotnet-core-cli
Applying Migrations - EF Core
Strategies for applying schema migrations to production and development databases using Entity Framework Core
doesn't seem to know the Migrate Methode
it's an extension method
hit
Ctrl+.
on itRelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade) Method (...
Applies any pending migrations for the context to the database. Will create the database if it does not already exist.
I did, but it did nothing, not even recommend searching on nuget
the link I gave will tell you the namespace you need to use then
apparently installed wrong version. Do I just call the Migrate (taht i made) in Program.cs? Or do I need to add it to teh build.services or to the app
I call it before
host.Run
or whatever starts your appalright
next Question, what do I send in for the IApplicationBuilder ? Doesn't seme to accept my WebApplicationBuilder
just change the parameter type
Sigh, so when I do so, I get this error
and I have the package installed (using Microsoft.AspNetCore.Builder;)
are you missing a using directive
don't think so no
found something else on the internet to perform self migrations, that one doesn't give erros
So I tried this but still doesn't seem to migrate
this looks like what i do for my dev/QA migrations, check the application logs to see what it's doing
i don't use it for prod because i've had too many odd issues to trust it generate sql scripts and run those manually
Is there a way to do this with docker? I thought it’d be easier to do all this with docker . It used to be so easy with the pre-made application we got in school but damn am I wrong haha. Just wanted a way to share my api with someone else who’s also working on the mobile app (a mobile app for a good cause for school) so she could also use it without having to explain her how to do a migration etc and manage a database as she doesn’t know all those things
docker isn't relevant to the problem, unless you want to create an image of the database with all the data already in it
probably better to figure out why your migration isn't running
there should be relatively verbose logging from EF core that shows it trying to find and execute migrations in your app
Fair, I’ll try to run it in debug tomorrow and see if that gives me anything and check the logs
I don't understand, no migrations seem to be happening when I run it in docker, but when I'm in vs debug, it runs it fine (welle xcept not finding the db bcz it doesn't exist but thats correct cuz the db doesn't exist with VS)
got all this stuff
after the db started, the next thing is
the code I run in Program.cs
nvm I didn't publish 😦
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.