Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No constructor with a connection string in Entity Framework datacontext

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).

like image 353
Alecu Avatar asked Jan 06 '13 12:01

Alecu


People also ask

What is DbContext in entity framework?

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.


1 Answers

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.

like image 99
scartag Avatar answered Sep 20 '22 08:09

scartag