Reputation: 1336
I have a project A and a project B. Project A has a dependency on project B. B contains a database file (.mdf) that I'd like to attach in A.
To do that I simply use
<add name="myDB"
connectionString="Data Source=(LocalDB)\mssqllocaldb;
AttachDbFilename=|DataDirectory|\myDB.mdf;
Database=some_alias_for_myDB;
Integrated Security=True" />
Then, I build project A and start it and it successfully uses myDB
. However, I want every time I start project A, to start with a fresh copy of myDB
. If myDB.mdf
was a part of project A, I would simply put a property Copy to Output Directory : Copy Always
on myDB.mdf
. In the case where myDB.mdf
is transitively received, the value of the property Copy to Output Directory
makes no difference to project A.
P.S. I also tried pre-build events to clean my project before building it - doesn't work because starting project A, when unmodified, does not trigger a build, therefore no cleaning is done.
Upvotes: 0
Views: 574
Reputation: 1336
In the end I decided to add a link to the common file. So in my project A I have a relative link ../B/myDB.mdf
. It is not the best solution, imo, since the project references now something outside of its domain folder, however it seems better than custom copy scripts.
Upvotes: 0
Reputation: 17248
Basically, that's normal behavior for data files. They're only copied to the target folder if the project is built, which only happens if the source has changed. You could tweak the build process to force a build each time, but that's not a nice solution and needlessly adds build time to your project when nothing has changed.
Probably the easiest solution for this problem is to copy the file before you open it, so that it is always restored to its original state on startup. Optimally, you even copy it to an user folder, because you will run into problems if you open a file in your bin folder in read/write mode once the program runs outside the debugger on a user computer (a program is normally not allowed to write to the "program files" directory or its subfolders).
Upvotes: 1