14

I am attempting to commit an update to my ASP.Net MVC project using GitHub integration in Microsoft Visual Studio 2017.

I am working on Code-First Migrations. After my previous successful commit, I performed the following commands on the Package Manager Console in Visual Studio:

PM> Enable-Migrations -ContextType ApplicationDbContext
PM> Add-Migration AccountNumberChanges
PM> Update-Database -Verbose

Once this was done, I tried perform a commit, but I was getting the following error:

Git failed with a fatal error.
error: open("MyProject/App_Data/aspnet-MyProject-
20171110110346.mdf"): Permission denied
fatal: Unable to process path MyProject/App_Data/aspnet-
MyProject-20171110110346.mdf

The above commands have added the .mdf file to the project's App_Data folder.

Morteza Azizi
  • 479
  • 5
  • 23
  • Did you try committing excluding that led file? I mean are you able to make other normal commits such a code change in the project? – Chetan Nov 13 '17 at 01:22

4 Answers4

17

You shouldn't be committing mdf files to git - they are data files for your database. git is likely complaining because the file is in use and it needs read access to be able to send the data to the repository. The simplest solution is to remove it from git with git rm. For example, at the command line:

git rm MyProject/App_Data/aspnet- MyProject-20171110110346.mdf
DavidG
  • 113,891
  • 12
  • 217
  • 223
12

If you still want to add your mdf files to git. I was having some trouble with this too, so what I found out is:

If you're using Server Explorer from Visual Studio, just right click on your Connection name in the Server Explorer window and click on Close Connection.

andrecj
  • 147
  • 1
  • 13
  • I'm not convinced. I'll have to try. But could you elaborate on what will happen if I close the connection? –  Dec 29 '17 at 18:57
  • @José Manuel Salazar Well, In my case, once I've done that I was able to commit it successfully – andrecj Dec 29 '17 at 19:39
  • 1
    Thank you! This worked perfectly for me! To anyone whose still wondering, closing the connection is just like "closing the file" - you're not loosing any data, and you can re-open (reconnect) as soon as the commit has finished. – Nik P Feb 11 '20 at 11:04
4

May be this will help someone in the future. The problem is that you still have the file connected to your Local DB in the Server explorer and the file has handles in use which blocks the commit to occur.

What I actually did was

  1. Right click the Local Db or the database to which the file is associated and

  2. choose Detach option so it can release any handles from the .mdf or .ldf files.

PS: you can attach the database anytime you wish to except during the commit timeframe.

Once the database is detached, the commit works without any problem. Try this and it works most of the time.

Jabez
  • 795
  • 9
  • 18
0

This just happened to me with a fresh install of VS 2019 on an older project that did not previously have this problem (maybe my ignore list is not correct). There was a MySQL db.lock file that was causing the problem.

In Team Explorer, right-click the file and select "Ignore this local resource".

worked for a quick fix (not saying it is the perfect or correct way but it worked)

Den
  • 26
  • 2