Reputation: 6680
So in Visual Studio I have a project on GitHub with an mdf file in the app_data directory.
How do I deal with this .mdf file, with regards to source control and building?
Bad Solution 1 - Check-in an empty .mdf file
I can create a stub mdf and check it in. But I don't like this solution. Once I check it in, then every change it shows up as changed and I have to exclude the .mdf on every subsequent check-in. Worse, if I forget to exclude it, then I have to revert the check-in.
Bad Solution 2 - Leave the *.mdf file ignored
I can leave the mdf file ignored. But now when someone checks out my solution an clicks build, the build fails. It is pretty easy to manually create the SQL file. But only if you know that is what you are supposed to do. Also, many potential users/contributors might check out my open source project, click build, see it fails, and decide to move on.
Currently I am using Bad Solution 1.
Mediocre Solution 1 - Copy another checked in file
Check in an mdf file to somewhere in the project. Run a copy command. I might have to settle for this option. I am hoping from something that is just a script thought.
Better Solution - Prebuild action to build mdf if missing I am struggling to implement this solution.
In the pre-build action of a Visual studio project, I have this:
IF NOT EXIST "$(ProjectDir)App_Data\SqlRepository.mdf" (
REM Command to create SqlRepository.mdf here
)
But Visual Studio is adding an *.mdf. There should be a script that will create the stub if the file is missing. What is Visual Studio doing?
So a valid answer will involve:
Upvotes: 1
Views: 267
Reputation: 6680
So my recommendation is to use Mediocre solution #1.
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="YOUR COPY COMMAND HERE" />
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="YOUR COPY COMMAND HERE" />
I thought I found a better solution recently, but I didn't. I was learning about Project and Item templates and the new dotnet version of those. Anyway, to add an MDF file in Visual Studio, I can right-click on a folder, choose Add | New Item ... and then select Service-based database. That means it is an item template. So I searched in the item templates and found this:
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Data\1033\EmptyDatabase\Database.mdf
So I can now just have a copy command. However, this isn't better than mediocre solution #1. There are issues with this solution:
Upvotes: 0