1

My solution have three projects:
enter image description here

  • dll Library that has the edmx EF Core used as DAL for Oracle database
  • library for business logic that references the mentioned above dll
  • ASP.NET Core webservice application that reference the business logic dll

All three target the same version of .NET framework: 4.6.1

To get the webservice to work and access the database I had to copy the App.config file from EntityFramework project to the Webservice project.

Everything was working fine until I decided migrating from ASP.NET 5 RC1 to ASP.NET Core 1.0.

After migration the web.config appeared in the webservice project root directory (other than the one that has been there under wwwroot folder) and I ran into the error mentioned here apparently because now I have two configuration files (app.config and web.config), the solution of removing the app.config made sense.

So I copied the all the sections from the app.config to the web.config and removed the app.config, which solved my first problem but now whenever I try to use the DBContext, I'm stuck with this exception:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No connection string named 'DBEntities' could be found in the application config file.

I noticed that the after publishing the application, the file myApp.exe.config does not have the connection strings any more. My question is simple, how do I get the application to look for the connection string in the web.config?!

I found some similer questions like this one, that and that but none of them has answers to my question.

Community
  • 1
  • 1
Gubr
  • 324
  • 1
  • 7
  • 15
  • With Webservice you mean SOAP/WCF? This scenario is unsupported in ASP.NET Core iirc. Only Rest (WebApi), MVC and SignalR (still in an early beta stage) are supported on ASP.NET Core – Tseng Aug 02 '16 at 07:26
  • @Tseng There is no WebAPI prject type in VS2015. Aren't all WebAPI in MVC? – Gubr Aug 02 '16 at 09:27
  • 1
    It's all in the same package and supported out of the box. Still, WebApi and MVC have slight differences in usage: WebApi routes usually have no actions and work with the http verbs and the returned `IActionResult` types are different in WebApi. Still no answer though, which of the technologies are you using as "Webservice". Webservice usually refers to WCF/Soap that's why I ask, since it's not supported in ASP.NET Core projects – Tseng Aug 02 '16 at 10:08
  • @Tseng it is http based webapi not wcf – Gubr Aug 02 '16 at 13:17

4 Answers4

4

I've been having the exact same problem, and the answer is IT'S BUGGY! In my case, my DbContext subclass is expecting a connection string in app.config, so that is what you must provide under <configuration>

  <connectionStrings>
    <add name="NameOfMyContext" connectionString="RemovedForSecurity" providerName="System.Data.SqlClient" />
  </connectionStrings>

Now, build and run the project. Go into the /bin/Debug/ folders until you find the yourappname.exe and the yourappname.exe.config files. Examine the yourappname.exe.config file. This is a weird blend of your various xml config files from your project. Make sure your connection string is in there.

I've had a very buggy experience with that file. Doing a "clean" doesnt remove it, so you can wind up in a situation where you remove all references to the connection string in your project (i.e. remove it from app.config), and your app still works because the string remains in that odd .exe.config file.

redwards510
  • 1,802
  • 1
  • 17
  • 23
1

Connection strings must be defined in appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "< connection string value here >"
  },

.. other stuff here
}

More at https://docs.asp.net/en/latest/fundamentals/configuration.html

Benjamin Abt
  • 1,730
  • 18
  • 33
  • I tried this before and it did not work. I have two connction strings, I use ReadOnly replication database for queries to enhance performance, and it is decided while creating the Context based on the need for data manipulation. – Gubr Aug 02 '16 at 09:24
  • The name of connection string is passed to the constructor of the DBContext – Gubr Aug 02 '16 at 09:31
1

If you have multiple projects set the project with the web.config file as the startup project.

Corical
  • 15
  • 5
njoromwando
  • 91
  • 1
  • 3
0

Another situation than you receive that exception is when you have written the <clear> tag after your connection string in the web.config of the MVC project.