简体   繁体   中英

What's the proper way to include Entity Framework connection string used in WCF service into a self hosting console application?

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?

According to your description and the issue you encountered, I think the problem boils down to the fact that the attached database is not properly attached to VS built-in database server instance. For this reason, I think we could configure the EntityFramework with VS built-in database instance string.

Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

And then override the seed method of the DropCreateDatabaseAlways/DropCreateDatabaseIfModelChanges class to provide the seed data.
Feel free to let me know if there is anything I can help with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM