updating a MySQL database from EF Core
Ive got a MySQL database i am accessing via Entity Framework Core using Pomelo.EntityFrameworkCore.MySql.
I initially generated the Data and Models based on the database, but now Ive made changes to the models (in c#) and i want to push those changes to the database.
How do i do this?
i found this but i think scafolding referss to pulling changes from the database. is that right?
GitHub
GitHub - PomeloFoundation/Pomelo.EntityFrameworkCore.MySql: Entity ...
Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector - PomeloFoundation/Pomelo.EntityFrameworkCore.MySql
16 Replies
also, the command they provide uses the connection string, something i would rather not do. i would rather get from our appsettings.json (yets i know, its not standard but its what we are doing)
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
so within my c# code i already access my connection string by pulling it form appsettings.json:
What am trying to do is take the structure defined by my models and enforce it onto the database, ideally using the connection string I've stored in appsettings.json instead of passing it
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
so i am not asking about how to acess my connection string from within the .NET project. i want to forward engineer a MySQL database based on the the structure defined in my models
am i missunderstanding your explanation?
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
The command for that needs access to the connection string
dotnet migration add Whatever
is the command if I'm not mistakenno way to pull it from configuration?
It takes the startup project as a parameter, in which case it will use the settings of that project iirc
see the docs of that command
this command has come up in my reasurech but i get this error when i run it from my project directory:
i was a total idiot.
project refers to a .csproj file, not the .sln file
ive got multiple data contexts, i assume the name of the context is the name of the class that impliments DbContext?
hm....
tried adding the flag
--context <nameOfClass>
but that threw the error: Unable to create a 'DbContext' of type '<nameOfClass>'. The exception 'Option 'name' not supported.' was thrown while attempting to create an instance.
hm... looks like i might need to create a factory of sorts...Design-time DbContext Creation - EF Core
Strategies for creating a design-time DbContext with Entity Framework Core
ok. i added this to my builder and tried to command again with the same error:
i also added an implimentation of
IDesignTimeDbContextFactory<MyChurchContext>
sln is for ides only to know what projects there are
well, that's not really true
it is used to some extent by tools
Visual Studio uses it to. or at least can read it
still havent solved this error
VS is an IDE
check it with the docs
it might actually not be able to use your host services
maybe only some of the commands do
I remember having to work around a thing that feels similar manually
I find that having a helper cli for stuff like this is good
like another app that has a reference to the code from the main project that sets up the connection string
and which can invoke the command correctly
this is infinitely better than hardcoding something, or repeating that same logic in a shell script
or obviously typing it out from memory each time
oh, though you miss spelled idea
a helper at the very least verify the model and the database match before booting would be great. but i need to figure out how to push my changes from my .NET project to the code first.