Failed to update database "XXX.MDF" because the database is read-only
May 13
ASP.NET, SQL Server 39 Comments
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!

Nov 09, 2006 @ 22:47:00
Thanks, it helped
Nov 18, 2006 @ 04:53:00
THANK YOU SOOOOO MUCH !
You don’t even know how much time you saved me !!!!!!
MAtt in Canada
Nov 21, 2006 @ 12:46:00
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
Nov 27, 2006 @ 10:56:00
thankyou, it’s very helpfull
Dec 05, 2006 @ 01:17:00
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!
Dec 12, 2006 @ 19:43:00
ThankQ! ThanQ!
This has been driving me nuts!
Jan 27, 2007 @ 08:03:00
Thank you, it is definite help in my case!
Mar 08, 2007 @ 08:06:00
heiiiii… it’s work!!!!..
Thank you so much.
erwin
indonesia
Mar 10, 2007 @ 21:53:00
Thanks for posting this. It solved my problem quickly
Mar 21, 2007 @ 03:32:00
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!
Mar 31, 2007 @ 09:51:00
Thanks!.. You are a lifesaver..
Easy guide without any unnecessary bullshit
/Johan
Apr 09, 2007 @ 05:53:00
Thanks for the help to what otherwise would be a PITA!
- Doc in Texas
May 03, 2007 @ 11:38:00
thank you very much my dear.
May 13, 2007 @ 13:18:00
Woah. This worked. Thanks. ^.^
Jun 24, 2007 @ 21:04:00
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!
Jul 27, 2007 @ 01:07:00
Thank you very much. Works for me. Before I found this posting I spent a few painfull hours
Aug 10, 2007 @ 09:37:00
Thank you very many for this help! -webmaster student from Finland
Aug 23, 2007 @ 12:26:00
Thanks a lot it worked out.
Rahul
Sep 13, 2007 @ 06:58:00
Excellent one
Oct 09, 2007 @ 19:38:00
Holy crap .. I have been struggling with this for days now. thank you so much for this!!
Oct 10, 2007 @ 13:16:00
THANK YOU VERY MUCH!!! it worked for me too. ADD NETWORK SERVICE, RESET IIS. Thats all!
Oct 18, 2007 @ 17:18:00
you are my hero, today
thx
Nov 29, 2007 @ 14:07:00
you and google are my best friends!
Dec 05, 2007 @ 01:48:00
Ta! Removed read-only and added NETWORK SERVICE did the job. didn’t even need to restart iis.
Jan 16, 2008 @ 18:10:00
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
Mar 17, 2008 @ 07:04:00
worked like a charm
Apr 16, 2008 @ 06:27:00
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
Sep 01, 2008 @ 14:01:00
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..!!!
Nov 10, 2008 @ 22:20:00
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
Feb 24, 2009 @ 05:42:00
Thanks a lot!
Mar 16, 2009 @ 01:50:46
What if this happens on a Windows application instead of a Web application? Any idea how to solve it?
Mar 23, 2009 @ 04:07:42
Thanks! It works!
Apr 16, 2009 @ 04:03:43
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.
Apr 30, 2009 @ 23:03:04
Big thanks!
Jul 31, 2009 @ 15:35:58
THX THX THX!!!
Nov 26, 2009 @ 16:06:01
Oh yes!
Did a website deploy and after I copied the files (including MDF) the “DB is readonly” error was displayed.
Thanks!
Dec 17, 2009 @ 13:36:37
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
Jan 01, 2010 @ 17:06:45
Big Big Big thanks for help me !!!
Mar 02, 2010 @ 10:51:07
Thanks a lot….. This worked for me…