1

I am using Entity Framework Code First to create my database.

Here is the current connection string

"Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;Data Source=(localdb)\\v11.0;Initial Catalog=Inventory"

However this database is not visible when I try to attach it inside SQL Server Management Studio.

This is because the account that runs the SQL Server service would need to have access to my user folder in order to see it.

I tried giving this account access but had problems due to permissions of other things in my user folder.

Thus I thought I should perhaps specify a folder name for the database to be created in, but I am unsure on how to do this, and what other problems this approach may bring.

[Update] I am now investigating setting the AttachDbFilename in app.config

this link is helpful however I aren't clear on how to set up |DataDirectory| for a winforms app.

[Update]

The following connection string works

 <add name="ConnectionString" connectionString="Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;Data Source=(localdb)\v11.0;AttachDbFilename=c:\databases\MyDatabase.mdf;"/>

It would be helpful to know how to configure the path to be the same as the exe file location.

Community
  • 1
  • 1
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    try to give relative path to configure the path same as exe file location connectionString="Server=.\SQLEXPRESS; Database=MyDatabaseName; AttachDbFilename=|DataDirectory|MyDatabaseName.mdf; Integrated Security=True; User Instance=True" providerName="System.Data.SqlClient" /> – Anshul Nigam Dec 10 '14 at 02:57

2 Answers2

0

You may first use sql server management studio (ssms) to connect to your localdb instance (server name: (localdb)\v11.0, Windows authentication)

make a backup of your localdb database (right click db -> task -> backup)

then share the db backup file with other system.

khchanel
  • 1
  • 1
  • Thanks @khchanel, I updated my question to better ask what I want. The idea is that I can distribute the app with out creating a back up because I am using code-first. – Kirsten Dec 09 '14 at 09:23
  • then you should research on using Entityframework, especially on migration & seeding topics. – khchanel Dec 09 '14 at 10:53
0

I wound up placing the following in Main()

AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);

and the following in app.config

 <add name="ConnectionString" connectionString="Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;"/>
Kirsten
  • 15,730
  • 41
  • 179
  • 318