Rhyous
Rhyous

Reputation: 6680

How to create an .mdf file from the prebuild action

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:

  1. mdf not checked in
  2. not a file copy of another checked-in file
  3. script builds mdf if not there
  4. Script is not overly complex
  5. Works with almost every other windows + visual studio environment

Upvotes: 1

Views: 267

Answers (1)

Rhyous
Rhyous

Reputation: 6680

So my recommendation is to use Mediocre solution #1.

  1. Exclude the mdf from source control.
  2. Create a templates folder
  3. Put an mdf file in the templates folder and check-it in.
  4. Add a Windows copy command to the rebuild to check if the mdf file is there and if not, copy it from the templates folder. <Exec Condition="'$(OS)' == 'Windows_NT'" Command="YOUR COPY COMMAND HERE" />
  5. Add a *nix copy command to the rebuild to check if the mdf file is there and if not, copy it from the templates folder. <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:

  1. A build agent doesn't have Visual Studio installed. Mediocre solution #1 is still good for this.
  2. What if Visual Studio is installed somewhere other than 'C:\Program Files'.
  3. What about supporting Linux/Mac? This file won't be there on those systems.

Upvotes: 0

Related Questions