The .mdf file is not created in ASP.Net MVC 4 after adding the <connectionStrings> tag to Web.config
I tried the ASP.Net MVC 4 tutorial . When I add the following code and create the application that I expected, the Movies.mdf file will be created in the App_Data strong> folder in Solution Explorer . However, nothing has been created in this directory. Any idea why?
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true" providerName="System.Data.SqlClient"/> <add name="MovieDBContext" connectionString="Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> Use: .NET Framework 4 VS 2010
The problem is resolved. The problem was the location of the project. I worked with files from a shared drive, which apparently did not create the database in the App_Data strong> folder . The connector code is absolutely accurate, as shown in the tutorial, and no changes are required.
Having a full copy of MS SQL 2012 on my machine, and not SqlExpress, I had this problem. I found that while continuing the application (instead of stopping it in the visual studio when VS catches the exception), I received a useful message in the browser:
A directory search for the file "c: \ users \ chris \ documents \ visual studio 2012 \ Projects \ MvcApplication1 \ MvcApplication1 \ App_Data \ ASP-MvcApplication1-20140225162244.mdf" failed with an operating system error 5 (access is denied.). CREATE a database crash. Some file names cannot be created. Check for errors.
which says the problem is that the sql service does not have write permissions to the directory of my application.
Granting permissions was a bit tricky because the required username did not appear (on Windows 8 Pro) in the GUI Explorer for security. The username for the default instance is for Sql Server NT SERVICE\MSSQLSERVER (for the instance it NT SERVICE\MSSQL$InstanceName ), and I had to enter it manually and then give it write permissions. I did this only in the App_data directory.
Then he worked.
This error occurs when Visual Studio and MSSQL work under different permissions. For this reason, your application does not receive write permission to create the .mdf database .mdf .
Decision
- Open
SQL Server Configuration Manager - Choose
SQL Server Services Go to the
Log On Astabthe value will be something like
NT AUTHORITY\NETWORKSERVICE(it depends)
As soon as you get the username,
- then go to the destination folder. (If by default,
App_Datain the project directory) - right click -> property windows -> security tab
click edit β check username or group
You will not see
Network Servicein the list of usernames or groups
All you have to do is add a username and grant write permission for that user or group.
This could be an example of AttachDBFilename ... I have this for my project, and it works great.
... connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Movies.mdf" providerName="System.Data.SqlClient" Also make sure your model is correct. If your model does not initiate this connection, it will not create a database.
I solved the problem by changing the connection string as shown below
<add name="MovieDBContext" connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> I installed Microsoft SQl Server Compact 4.0 from nuget .. but I'm not sure if this is really required to install it or not.
In this MVC tutorial, just add " Initial Catalog=Movies ;" (without quote) in the connection string , and it will work well.
<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>