How to Add ASPNETDB.MDF to Local SQL Server

Just recently I was working on a MVC Application ,which had AccountController along with a Model class having Account details , such as UserName,Password,EmailAddress. In order to get started I got Nuget to get to install Entity Framework and EFCodeFirst usign Package Manager Console. After doing so , I started debugging my application . This worked fine. I created a controller with Create, Edit,Details ,Delete action methods . After adding some data in my app, I was a bit confused as to wear did my data go? As I wasn’t able to find it on my local SQL Sever.
Next step was to check my webconfig file …as this was the place where we have all the info about the connection strings.

    <connectionStrings>
     <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
             providerName="System.Data.SqlClient"/>
      </connectionStrings>

So atlast I found out that the data is going into aspnet.mdf which I found was there under my App_Data Folder 





 

Okay there I go. I see the file in there, so I double clicked on the file to open up my Server Explorer which had the database which was created automatically.


 Wow that seems just awesome isn’t it? Well I than thought why not take a backup from here of this data and put it in my local SQL Database. Did some research to find out, and option called as Publish to Provider which would help me generate SQL Scripts for all the ASPNETDB.MDF Database objects, as you see we have the Tables, Views, and Stored Procedures etc.

In order to do so right click on ASPNETDB.MDF which gets you the below 




  Once you have got this the later is every simple , you just have to follow the instructions as to where you need to save that script and then finally open the script in SSMS and run ( execute it) so as to restore all our SQL Objects onto our local sql instance
Last and not the least, do not forget to go back and make change to your connection strings in your webconfig J





No comments:

Post a Comment