Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

Entity Framework connection string in WCF console host

$
0
0

I'm doing my homework, a WCF service that uses SQL Server with Entity Framework, hosted through a console application, and using a WPF client.

There are 3 different projects, and the host and the service is in the same solution. I've included the Entity Framework connection string in the console hosts' `app.config` file from the `web.config` file from the service. This way the server and the host throw an exception when I try to make a query:

System.Data.Entity.Core.EntityException: 'The underlying provider failed on Open.'

The inner exception says:

SqlException: An attempt to attach an auto-named database for file C:\Users\username\source\repos\BlogAppWcf\BlogHost\bin\Debug\BlogDb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

So it basically searches for the `.mdf` file in it's own project folder, while it's inside the service's `App_Data` folder.

The original connection string looks like this, I copied this to the host's `app.config` from the `web.config`:

connectionString="metadata=res://*/BlogDbEntities.csdl|res://*/BlogDbEntities.ssdl|res://*/BlogDbEntities.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=(LocalDB)\MSSQLLocalDB;
attachdbfilename=|DataDirectory|\BlogDb.mdf;
integrated security=True;
MultipleActiveResultSets=True;App=EntityFramework""

I've tried modifying the `AttachDbFilename` attribute in the `app.config`, I gave it an absolute path like this:

attachdbfilename=C:\Users\username\source\repos\BlogAppWcf\BlogAppWcf\App_Data\BlogDb.mdf;

and this way it works like a charm! No more exceptions on queries.

But this isn't the right way to do it, especially because I have to send it to my teacher. I want to give it a relative path, just like this:

attachdbfilename=..\..\..\BlogAppWcf\App_Data\BlogDb.mdf;

but it doesn't work this way.

Has anyone got any suggestions, maybe I'm doing or thinking something completely wrong?


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>