I am using Entity Framework 5.0
for my project. I looked on the internet and I saw that for the entity framework data context there was another constructor that had a string parameter for the connection string.
On my generated data context
I don't have such a constructor. I looked into the base DbContext
and it has such a constructor.
Was the code generated wrong? I generated the code from a database. Could this be the cause?
Turns out that I can edit the code generation template file to add the new constructor. Now I have added the new constructor. The file is a MyDataContext.tt file under your edmx model. There you have c# code mixed with template code. You can copy the no argument constructor from there and paste it bellow. Then you can change it and add a string argument to it and pass that argument to the DbContext constructor like this : base(myString).
Definition. A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.
You can add one as needed.
Check the generated file and add an overloaded constructor.
public YourContext(string connectionStr)
: base(connectionStr)
{
}
Probably better to define this in a partial class though, as every generation will require you to manually add it each time.
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