Using ASP.NET Core and EF Core, I am trying to apply migrations to the database. However, the login in the connection string in appsettings.json
that the app will use has only CRUD access, because of security concerns, so it can't create tables and columns, etc. So, when I run:
dotnet ef database update -c MyDbContextName -e Development
I want to tell it to use a different connection string, but I don't know if this can be done? Basically, I want to use two different connection strings, one for deployment and one for running the app. Is this possible? Is there a better approach? Thanks.
Update the tools Use dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.
Update Data With Entry() Method In EF core Because you're creating a new instance (which isn't tracked) instead of updating the existing instance (which is tracked).
Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.
In EF Core 5.0, you will pass the connection string in the command line like this,
dotnet ef database update --connection "connection string"
Reference: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#new-command-line-parameters-for-namespaces-and-connection-strings
Keep both connection strings in appsettings.json
. Inherit a child context class from the main one and override OnConfiguring
with another connection string:
public class ScaffoldContext : MyDbContextName { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string scaffoldConnStr = ConfigurationManager.ConnectionStrings["scaffoldConnStr"].ConnectionString; optionsBuilder.UseSqlServer(scaffoldConnStr); } }
Then use:
dotnet ef database update -c ScaffoldContext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With