16

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

Alecu
  • 2,627
  • 3
  • 28
  • 51
  • 1
    Can you post the codes to make the difference more clear ..? – Bhushan Firake Jan 06 '13 at 12:31
  • What is stopping you from adding it manually? – Paul Fleming Jan 06 '13 at 12:34
  • Also, DbContext is "Code-First". If you have an existing database, the typical usage is "Database-First". – Paul Fleming Jan 06 '13 at 12:35
  • @flem With EF5 even database first generates the dbcontext classes for you. It uses T4 files – scartag Jan 06 '13 at 12:38
  • @scartag. I thought db-first used `ObjectContext`? Did it change in EF5? – Paul Fleming Jan 06 '13 at 12:40
  • @flem going forward we are advised to use dbcontext and with EF5 when you generate the classes, it uses T4 to give you that. although you can still access objectcontext from dbcontext if you need to. – scartag Jan 06 '13 at 12:41
  • the DbContext which is the base class for my datacontext has the property ObjectContext inherited from the interface IObjectContextAdapter that the DbContext implements. But it's implemented private so I actually have to cast the db context to the interface to access it. I know that there is a entityconnection string builder class but it too much for my requirements. – Alecu Jan 06 '13 at 12:54

1 Answers1

23

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.

scartag
  • 17,548
  • 3
  • 48
  • 52
  • I believe I can also edit the .tt file but I don't know if this file is a autogenerated too. – Alecu Jan 06 '13 at 12:57
  • 1
    tested and changed the .tt file adding a new constructor in it. works now – Alecu Jan 06 '13 at 13:04
  • 1
    @Alecu: I had the same problem and did the same thing: good to know! – sthiers Jun 11 '13 at 09:52
  • 3
    I agree with scartag it is better to define it in a partial class, as every-time your code is generated you will loose your customisations. And it's easier to do that than edit the tt files. – Alex KeySmith Sep 04 '13 at 11:05
  • 1
    NOTE: In order to set the ConnectionString programatically, make sure you don't format it like the one in web.config or app.config. That one will contain " which needs to be changed to single quotes, or you will get a most unhelpful error message. http://stackoverflow.com/questions/6997035/keyword-not-supported-data-source-initializing-entity-framework-context . Either that, or use the EntityConnectionStringBuilder. – Pittsburgh DBA May 19 '15 at 14:22