Can I commit changes to actual database while debu

2019-01-15 20:46发布

I am creating a C# application using Visual Studio that uses an SQLExpress database. When I hit f5 to debug the application and make changes to the database I believe what is happening is there is a copy of the database in the bin/debug folder that changes are being made to.

However, when I stop the debugging and then hit f5 the next time a new copy of the database is being put in the bin/debug folder so that all the changes made the last time are gone.

My question is:

Is there a way that when I am debugging the application I can have it make changes to the actual database and those changes are actually saved or will it only make changes to the copy in the bin/debug folder (if that is what is actually happening)?

I've seen similar questions, but I couldn't find an answer that said if it's possible to make those changes persistent in the actual .mdf file.

The reason I ask is because as I build this application I am continuously adding pieces and testing to make sure they all work together. When I put in test data I am using actual data that I would like to stay in the database. This would just help me not have to reenter the data later.

Thanks in advance for any help or information that could help me better understand the process.

3条回答
放我归山
2楼-- · 2019-01-15 20:55

Select the databases in Solution Explorer and change the "Copy to Output Directory" to "Copy if newer". Later, when you finally get the final version, copy paste the databases in debug folder to your source folders

查看更多
再贱就再见
3楼-- · 2019-01-15 21:03

The problem with 'Copy if newer' is that when you make changes to any part of the structure of the database, that change is to the version in your Project directory and not to the version you are building on in the Debug directory.

Also, when you try a non-debug run you are not using the copy you have been chaging in the debug location.

I normally use a Postgresql, but as this is a new computer and I discovered MS do this Lite version, decided to give it a try. I have a lot of data which is historic that I want in the table, then some manual manipulation with a bit of SQL, Then I can carry on creating the actual functionality of the database.

You can not just copy the altered version into the project directory, because there is a log file and no one seems to know what is in there but Microsoft desperately need that to match your database file in some way. (I am guessing you already tried that too).

I didn't want to have to start from scratch every time I ran it, and just like you I found no solutions. So in a desperate move I copied the version in Debug to a tmp directory. Then deleted the version in the project solution explorer.

Then I dragged and dropped the version in tmp into solution explorer. (Everything in it is structurally the same as the original, except as stated below)

That did it, it recreated that Log file. Now my debug sessions start with a ready loaded data.

Words of warning. 1) Changing the database structure. In your solution explorer, the the Linq to SQL DBML file, if you have changed some structural part of the database in code (which is where I tend to do it), right click on the DBML file and go into design view. Then drag into the design any new tables or views that you have added.

2) Because having to delete and drag in the database feels a bit dodgy, write yourself a function that runs on Form_Open when in debug mode that copies your database file such as

\MyDatabaseBackup\myDatabase_date_time.mdb

Remembering that if you do go back to a previous version at any time you need to rename it myDatabase.mdb

查看更多
冷血范
4楼-- · 2019-01-15 21:09

Copy database to some proper location and change the connection string in config file to point to that only, it is only a suggestion as not sure what type of app. it is.

查看更多
登录 后发表回答