You add a data file to your project and that is a source file, just like any other. You don't connect to that file at run time because you don't want to fill it with test data and potentially delete some default data. That data file should remain clean.
When you build, the clean source file is copied to the output folder along with your EXE by default. It's that copy that you connect to at run time. You can make as much of a mess of it while testing as you like, safe in the knowledge that your source file is unaffected. If it's a Release build, you have a clean database to deploy with your app.
By default, the Copy to output directory
property of the data file is set to Always copy
. That means that every time your project is built, a new copy of the data file is made in the output folder. If you run your project twice without making any code changes, there is no build so there is no new copy, so any changes made during the first run will remain. If you make a code change though, the project is built before the second run so your data file is overwritten with a new copy, so your changes will be lost. It's that behaviour that confuses people who either don't read about how to manage local data files or don't read it properly. Your changes ARE saved. They are simply overwritten next time you build.
If you set Copy to output directory
to Copy never
then no database is copied to the output folder. If your code then tries to connect to a database in that location it will obviously fail. You would need to copy the data file yourself to that location or, if you change the connection string, to another location.
The most sensible option is to set Copy to output directory
to Copy if newer
. In that case, a new copy of the database will not be made unless you've made a change to the schema or default data in the source data file. That means that changes made while debugging will survive between sessions. You may find that a new copy is made sometimes when you don't want it to but that's still better than the alternative, which is making a new copy every time. If you don't touch the source data file, which you have no reason to do if you're not changing the schema or the data, then your test data will survive across sessions.