C
C#4d ago
hutoanhill

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
hutoanhill
hutoanhillOP4d ago
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
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
hutoanhill
hutoanhillOP4d ago
so within my c# code i already access my connection string by pulling it form appsettings.json:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMySql("name=MyChurchConnection", Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.35-mysql"));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMySql("name=MyChurchConnection", Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.35-mysql"));
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
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
hutoanhill
hutoanhillOP4d ago
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
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Anton
Anton4d ago
The command for that needs access to the connection string dotnet migration add Whatever is the command if I'm not mistaken
hutoanhill
hutoanhillOP4d ago
no way to pull it from configuration?
Anton
Anton4d ago
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
hutoanhill
hutoanhillOP4d ago
this command has come up in my reasurech but i get this error when i run it from my project directory:
No project was found. Change the current working directory or use the --project option.
No project was found. Change the current working directory or use the --project option.
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...
hutoanhill
hutoanhillOP4d ago
ok. i added this to my builder and tried to command again with the same error:
builder.Services.AddDbContext<MyChurchContext>(options =>
options.UseMySql("name=MyChurchConnection", ServerVersion.Parse("8.0.35-mysql"))
);
builder.Services.AddDbContext<MyChurchContext>(options =>
options.UseMySql("name=MyChurchConnection", ServerVersion.Parse("8.0.35-mysql"))
);
i also added an implimentation of IDesignTimeDbContextFactory<MyChurchContext>
Anton
Anton4d ago
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
hutoanhill
hutoanhillOP4d ago
Visual Studio uses it to. or at least can read it still havent solved this error
Anton
Anton3d ago
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
hutoanhill
hutoanhillOP3d ago
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.
Want results from more Discord servers?
Add your server