Accessing DbContext From a Different Project

Today, I discovered in interesting pitfall when accessing the database from different projects (inside one solution). I have defined connectionString in the Web.config file. To my dismay, this was not sufficient if you want to use your data context in different projects.

Specifically, I was greeted by "Connection could not be opened" error. The inner exception the program was trying to access YourNamespace.YourEntities which of course doesn't exist. Instead, one has to define the same connection string (with the same name) inside Web.config or use Data Source attribute inside DbContext like so:

Leave Comment

Your email address will not be published.

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box