Reputation: 2138
I'm trying to use a local db for my application, and want it to reside in a folder inside my application. Unlees I'm missing something, this shouldn't be a problem with user rights, since it is in the application folder.
The connection string is this:
<connectionStrings>
<add name="Calendario2DB"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=\AppData\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I'm Using a code first aproach, so the database should be generated (filled) at run time, but I have a problem with
AttachDbFilename=\AppData\Database1.mdf
This should be pointing to a folder in my app (called appdata) but is not working with an error:
A file activation error occurred. The physical file name '\AppData\Database1.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
So how do I have to write the path name to the phisical file?
Upvotes: 3
Views: 3588
Reputation: 54638
You should use the built in |DataDirectory|
feature.
<connectionStrings>
<add name="Calendario2DB"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then the database would be located in:
\Program Files\Application Location\App_Data\Database1.mdf
According to the documentation you cannot use relative paths without using |DataDirectory|
:
The path may be absolute or relative by using the DataDirectory substitution string. If DataDirectory is used, the database file must exist within a subdirectory of the directory pointed to by the substitution string.
Upvotes: 4