Failed to update database “XXX.MDF” because the database is read-only

After a few hours of Googling and testing things I finally solved this problem so I thought I’d write the solution down both for my own and other’s benefit.

What I did to get the error:
I had just done some changes to one of my web sites using Visual Studio 2005 and ASP.NET 2.0 with VB.NET. I have previously used MySQL as a database for this site but thought I would test using SQL Server 2005 Express Edition for a new feature I just added.
As usual I did Build – Publish Web Site and then copied and pasted the published directory to my web server. When browsing any of the web pages that use the SQL Server 2005 database I get this error message: Failed to update database “XXX.MDF” because the database is read-only.

Solution:
From what I found on Google this seems to be a confirmed bug in the beta releases of Visual Studio but should be fixed in the released version, but obviously it is not. There seems to be many ways and suggestions on how to fix this, some very complicated.

This worked for me:
1) Make sure App_Data directory or any contained files does not have file system attribute Read-only set. (I had some files marked as Read-only)
2) Give user ASPNET and NETWORK SERVICE Modify control over the App_Data directory. (I had to add both these)
3) Run IISRESET to restart IIS to refresh its permissions.

Main part of this solution was found in this MSDN forum thread, specifically posts by Thongtap and Justin. Thanks guys!

43 comments

  1. Thank you Very much,

    I’m strugeling already over 2 months with SQL 2005 rights when I want to put my sites online. This works great!!

    Jelger ITND the Netherlands

  2. A big “thank you” to myself for documenting this. I just had this problem again and had completely forgot how I solved it the last time… Then it hit me – I have the solution in my blog! 🙂

  3. Very helpful!

    I only gave the ‘network service’ user modify permissions for the app_data folder and subdirectories.

    Then i ran iisreset and it worked 🙂

    Thanks!

  4. can anyone please help me to set the permissions, so that database wont remain read-only? i’m new to this stuff and am at a complete loss!

  5. Could not remove read only, it would come back every time as half greyed out (due to parent propagated permissions.)

    But YES, “ADD NETWORK SERVICE, RESET IIS. Thats all!”

    This did it1

  6. Thanks a lot… this really worked. However, I noticed that granting permissions for ASPNET account was only necessary. There was no need for the NETWORK SERVICE one.

    regards

  7. i’m doing it by

    right click the folder App_data, click properties,
    select security,
    click edit,
    and then add the user names : NETWORK SERVICE

    after that enabled the modify check box….

    reset the IIS…

    and..

    Yeah…it’s Work..!!!! 😀

    Thank you Very much..!!!

  8. you can use command line commands in your deploy script:

    CACLS deploy\App_Data /e /p ASPNET:f
    CACLS deploy\App_Data /e /p “NETWORK SERVICE”:f

  9. I encountered the same problem with a Windows application on Vista after applying SQL Express SP3. For some reason, the account used to access the database no longer has write access to the database. To overcome this, I manually granted write access to the Windows Users group on the database. That seems to do the trick, but I’m not sure whether it’s a good security practice.

  10. I don’t know how to do “Give user ASPNET and NETWORK SERVICE Modify control over the App_Data directory. (I had to add both these)
    3) Run IISRESET to restart IIS to refresh its permissions.” steps

  11. this same problem is occurred in my windows database application .Could you please give your suggestion.

  12. What if we are having desktop application with this same problem? It does not need app_data and any server except sql…

Leave a comment

Your email address will not be published. Required fields are marked *