Relative Path in ASP.NET Web.config - asp.net

I have the following tag in my ASP.NET Web.config application:
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="C:\Documents and Settings\jdoe\Desktop\LcdManager\LcdManager\App_Data\monitor.sqlite"'" providerName="System.Data.EntityClient" />
that refers to an EntityFramework instance mapping a sqlite db located in the App_Data directory of the application.
How can i make the link relative so that deploying in a production environment is easier?
Thanks

Have you tried
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="./App_Data/monitor.sqlite"'" providerName="System.Data.EntityClient" />
or
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="|DataDirectory|/monitor.sqlite"'" providerName="System.Data.EntityClient" />

Can't you use "|DataDirectory|" in the connection string (instead of C:...\App_Data)? E.g:
connectionString="... |DataDirectory|\monitor.sqlite ..."
I know this works for SQL Server Express databases that are deployed in the app_data folder of the application. But I don't know if this works for Entity Framework / SQLite.

I assume you're talking about this bit
C:\Documents and Settings\jdoe\Desktop\LcdManager\LcdManager\App_Data\monitor.sqlite
instead, try this
../../App_Data/monitor.sqlite
(not sure about the number of '../' but you get the idea
../../App_Data/monitor.sqlite

Related

Why is my web.api project still looking for an .mdf file?

I have a code first web.api project that was working fine with the automatically created .mdf in the App_Data folder. I decided to move my app to IIS and modify the app pool config to load the user profile, again, no problem. Then I loaded the .mdf in visual studios sql explorer and at that point, the app pool login from the web app started failing. No clue why and couldn't fix it, so I decided to use sql express instead of waste more time on it. So, I installed SQL Express, killed all my migrations, and modified the web.config connection string to:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
When I try to use migrations to update-database or run the api app, I get an exception saying
Cannot attach the file
'C:\Users\Source\MyApp\Main\Solution\MyApp.API\App_Data\MyApp.DataService.DataModel.AppEntities.mdf'
as database 'MyApp.DataService.DataModel.AppEntities'.
Well, that makes sense since I deleted it, but why is my app still trying to connect to the .mdf file? It's gone and I've changed the default connection. I've searched the solution for anything referencing the .mdf file, but nothing shows up. What am I missing?
I finally figured out that I had to name my connection string the same as my DbContext class, so in my case:
<connectionStrings>
<add name="AppEntities" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
to match my DbContext derived class
namespace MyApp.DataService.DataModel
{
internal class AppEntities : DbContext

ASP.NET MVC 4 application database connection issue after session timeout

I have an MVC 4 website using .NET framework 4.5 that works fine at first. It allows me to login and go about my business adding, deleteing and editing records. As such, it is obviously initially connecting to the database without a probelm. However, if I leave it inactive for a while and come back at a later time, and then click a link to a page that shows data from my database, I get the following error:
The SSE Provider did not find the database file specified in the connection string. At the configured trust level (below High trust level), the SSE provider cannot automatically create the database file.
The website works fine on my localhost, but not when I host it online. I use shared hosting for this. I've done extensive searching online and found lots of people getting the same error, but they don't seem to have a website that works initially and then suddenly stops working. The solutions I've found online say something along the lines of:
add these two lines to your web.config file
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=p3swhsql-v15.shr.phx3.secureserver.net; Initial Catalog=DatabaseName; User ID=****; Password='****';"/>
by default the .net site is trying to find your database in the local filesystem in this case would be the app_data subfolder under the wwwroot directory. simply removing the connectionstring and replacing it with the database connection solves the issue. My example utilizes an SQL db on godaddy's server. however i'm sure you can replace the connection string with the appropriate string for your connection. good luck and i hope this helps anyone that may be having this issue
What my program seems to be doing is using the online database when I first login, and then reverting to trying to use the database in the local filesystem once the session timesout. This is very frustrating. My web.config file looks as follows:
<connectionStrings>
<remove name="DefaultConnection"/>
<add name="DefaultConnection" connectionString="Data Source=winsqls01.cpt.wa.co.za;Initial Catalog=SportFantasySA;Persist Security Info=True;User ID=*****;Password=*****" providerName="System.Data.SqlClient"/>
</connectionStrings>
Please help. I'm desperate to get this working. I understand that it can't create the sql express database in the hosting space, that's why I'm using the connection string for the database on my hosting company's server.
If you have hosted it on Godaddy go for the connection string as
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider" connectionStringName="LocalSqlServer"
..other attributes here... /></providers>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=xxxx; Initial Catalog=xxx; User ID=xxx; Password=xxx;"
providerName="System.Data.SqlClient" />

Connectionstring to other projects' App_Data folder

I have a asp.net webforms project with a database in the App_Data folder referenced by connectionstring:
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
The solution also contains another project with webservices that need to access the same database.
Is it possible to write a relative path to this database in the connnectionstring? - eg. something like this(does not work):
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|/../../ProjectName/App_Data/database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
Basically |DataDirectory| is an alias for App_Data folder. Since you are not using App_Data in your services project, it would not help you in this situation.
What you can do though is try starting from the project root using ~\:
AttachDbFilename=~\..\ProjectName\App_Data\database.mdf
Also there is always an option to use an absolute path:
AttachDbFilename=d:\Projects\ProjectName\App_Data\database.mdf
And finally there is always an option to build connection string programmatically.

asp.net mvc Invalid value for key 'attachdbfilename'

I am currently reading Manning's "ASP.NET MVC 4 in Action" book and trying to get the first example to work.
Within my test application, I built a simple Model and created some Views. I then imported "SQL Server Compact" using NuGet.
When I finally try to run the application I get the following error:
Invalid value for key 'attachdbfilename'
This occurs on every interaction with the Database (SELECT or other CRUD operations) I am running. Any ideas?
Though, I am bit late to respond to this question but I was facing the same issue and I resolved it by modifying the connection string like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Hope this would be helpful for others too.
I tried finding a solution for this particular error without success.
Fixed it in the end by updating the .Net Framework 4 to 4.0.2. Patch and details can be found Here is the link
Hope it helps
I think the problems here is string in web.config is not correct
According to your set up sql, the string will work if you set something similar like this
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Data Source=.\SQLEXPRESS; --> sometime, it will be Data Source=.;
after that, you need config right to access FOlder App_Data
If u test on Window 7, right click folder. property
Security tab -> add user network Service with full right
Go to section of Web.config and modify the section to the following to get SQLServerCE4 to be used:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
I guess problem occurs only when using SQL Express on .NET Framework v4.0.30319 anyways
SQL Server Express Local Database Runtime support in SqlClient is added in .NET Framework update 4.0.2 read - http://support.microsoft.com/kb/2544514
Alternately use connection string similar to this
<add name="EmployeeDbContext" connectionString="Data Source=.;Initial Catalog=Employees;AttachDBFileName=|DataDirectory|\Employees.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
above connection string was giving an error but as soon as added"\" before the name of my database the error got resolved.
Try this:
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
You are using SQL Server Compact 4.0:
Be sure you have installed SQL Server Compact 4.0.
Install VS 2010 SP1 Tools for SQL Server Compact.
Change your connection string to:
<connectionStrings>
<add name="MyModelDBContext" connectionString="Data Source=|DataDirectory|mymodel.sdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Right click on controllers folder and -> add -> controller
Type:
YourController
MVC-Controller with read/write ...
MyModel
new datacontext -> APP.Models.DatabaseContext
Here is a blog post I found about that error. It is a bit old, but uses the express version of sql server. MSDN Link
That blog post talks about expecting the server name of the sql database to be a local address and not /sqlexpress
There is also this blog post that talks about having an incorrect connection string. So maybe check your connection string to the database. and your problem could be there.
Facing the same issue, I have looked over my connection string and found that "UNC-style" paths e.g. \\MyServer\MyDBServer are not allowed, one has to use the MyServer\MyDBServer syntax for a remote host or the .\MyDBServer syntax for locally hosted DB Servers.

"The connection string specifies a local Sql Server Express instance", Except it Doesn't

I'm trying to deploy an asp.net application to a server using a SQL Server instance for the ApplicationServices membership database.
The problem is, I'm getting an error that says
The connection string specifies a local Sql Server Express instance
using a database location within the applications App_Data directory
I got this error when I initially tried to deploy the aspnetdb.mdf itself with the application.
I got this error when I then scrapped that plan and decided to do a web.config transform so that in Debug I use the Express database, but on Release the connection string goes to SQL Server.
I got this error again when I decided out of curiosity to remove all references to the express database from the code, so there could be no possible way anything would be looking for the Express database. No luck.
Does anyone have any ideas about this? I have deleted and re-installed the web site in IIS each time, noting that there is no App_Data being deployed and no mention of the .mdf file in web.config - to no avail. It still thinks there's a connection string telling it to look for a SQL Server Express database :/
Edit: Here's the connection string I'm using. Pretty standard, I think, but I could always be wrong.
Data source=HERP;Initial Catalog=DERP;Integrated Security=True
Could the error refer to the default connection string from the machine.config file (the LocalSqlServer one)? This could be happening, considering that the default membership provider uses this connection string:
The following default membership element is configured in the
Machine.config file [...]:
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" [...]
connectionStringName="LocalSqlServer" [...]
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Resources