Missing ConnectionString parameter in Entity Framework Constructor

15. März 2016

After working several years with LinQ2SQL and the DBContainer classes I recently switched over to Entity Framework.

When starting my new WebApplication project I compared LinQ2SQL, ADO.Net and EF6 with some possible database operations and finally decided to pick EF as it got the best results. (Details and my regrets may be following up on another blog post).

So when working with EF I also decided to place all files in a seperate project as I had to share the classes among several subprojects. Unfortunately this construct ends in not sharing application settings (app.config / web.config) so I was looking for some way to pass a connection string along with the constructor, as the web.config settings wouldn’t be recognized.

Thanks to StackOverflow and Google I found the most obvious:
The EF-context files inherit from DBContext which owns such an constructor. But the automated generated files lack of this constructor.

Blog_EF_001

So the obvious is (shame on me that I didn’t saw this in first place) to add the missing constructor by hand:

Blog_EF_002

But unfortunately this file is auto-generated – it will be overwritten each time you change something in the designer.

So to prevent the unwandet deletion of your manual constructor is easy if you know how: All you need is a partial class which simply adds the missing constructor:

Blog_EF_003

And that’s it… 🙂

Advertisements