C
C#2y ago
Temptica

❔ 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
builder.Services.AddDbContext<UserContext>(option => option.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString")
builder.Services.AddDbContext<UserContext>(option => option.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString")
It works without docker
27 Replies
jcotton42
jcotton422y ago
by "the model has not been created" do you mean the migrations aren't running?
Temptica
TempticaOP2y ago
seems liek it yes
jcotton42
jcotton422y ago
you must tell EF explicitly that you want to run migrations $migrations
MODiX
MODiX2y ago
Applying Migrations - EF Core
Strategies for applying schema migrations to production and development databases using Entity Framework Core
Temptica
TempticaOP2y ago
doesn't seem to know the Migrate Methode
jcotton42
jcotton422y ago
it's an extension method hit Ctrl+. on it
Temptica
TempticaOP2y ago
I did, but it did nothing, not even recommend searching on nuget
jcotton42
jcotton422y ago
the link I gave will tell you the namespace you need to use then
Temptica
TempticaOP2y ago
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
jcotton42
jcotton422y ago
I call it before host.Run or whatever starts your app
Temptica
TempticaOP2y ago
alright next Question, what do I send in for the IApplicationBuilder ? Doesn't seme to accept my WebApplicationBuilder
jcotton42
jcotton422y ago
just change the parameter type
Temptica
TempticaOP2y ago
Sigh, so when I do so, I get this error
Temptica
TempticaOP2y ago
and I have the package installed (using Microsoft.AspNetCore.Builder;)
jcotton42
jcotton422y ago
are you missing a using directive
Temptica
TempticaOP2y ago
don't think so no found something else on the internet to perform self migrations, that one doesn't give erros
Temptica
TempticaOP2y ago
So I tried this but still doesn't seem to migrate
Jimmacle
Jimmacle2y ago
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 when generate sql scripts and run those manually
Temptica
TempticaOP2y ago
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
Jimmacle
Jimmacle2y ago
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
Temptica
TempticaOP2y ago
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)
Temptica
TempticaOP2y ago
got all this stuff
Temptica
TempticaOP2y ago
after the db started, the next thing is
Temptica
TempticaOP2y ago
the code I run in Program.cs
Temptica
TempticaOP2y ago
nvm I didn't publish 😦
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server