I am using EF and in MY web config I have removed the Connection string after adding the model for the first time. So it looks like this now:
<connectionStrings> </connectionStrings>
I have modified my Model.context.cs file where I have added one parameter in the Constructor of Entities. Below is the code:
public QMatchServerEntities(string connectionString)
: base(connectionString)
{
}
When I make connection and create object of entity I send the whole connection string in the parameter. I used this case because it was a requirment from client side that DB password should be encrypted in the code.
Model.QMatchServerEntities qMatch = new Model.QMatchServerEntities("Connection string")
What I wanted to know is that, after this when I update the model it ask me to create new connection first because EF is unable to find the connection string of this model in web config. Is there any way to fix that?
Thank you.
2
Answers
You can implement the migration of the database during the startup of you web app.
I presume you using .NET 6.
First, create a new class named DbInitializer.cs, in your project like this :
After that, in you Program.cs file add the code mentionned under "Add this part" :
Don’t forget to add the dependencies.
Tell me if it works or not.
You need to instruct the DBContext to use that provide connection string, so your code should override the OnConfiguring(…) method somewhat like this:
You can find more information over here: https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/