Create an ASP.NET MVC Data-based Web App Pt 2

If you are following along with this tutorial, in my previous post, Create an ASP.NET MVC Data-based MVC Web App Pt 1, I showed you how to create models in the models folder, how to display a list of Fabrics on the home page using the Home Controller, and set up a Data Connection with localdb in VS 2017.

Continuing on from the previous post, we are ready to enable Database Migrations.

Working with Data Pt 1 - Database Migrations

Step 6: Configure connection string in MyFabricStashAppDb. In the web.config file. Add a connection string. One should have been created for you called "Default Connection". We will just modify the one given. We are going to tweak it a little bit.

Change your connection string as follows:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;
AttachDbFilename=|DataDirectory|\MyFabricStashAppDb.mdf;
Initial Catalog=MyFabricStashAppDb;
Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  • Then, add a line of code in your MyFabricStashDb.cs file, that uses the Default Connection string in the web.config file.
public MyFabricStashDb() : base("DefaultConnection")
{

 }

*A little tip: to create a constructor, start typing "ctor" and then tab twice. VS will create the constructor stub for you.

Your DbContext class should look like this:

Note-If you run the application again, you will see a blank page, that is because we are pointing to a new database and that previous data is no longer available.

If you look at the Server Explorer, you should now see DefaultConnection in your Data Connection list. You may need to refresh the window to see the DefaultConnection.

If you look in the Solutions Explorer, in the App_Data folder, you should see your .mdf files. The most important one being the one you created with the Default Connection string in Step 6. You may have to click "Show All Files" in order to see the mdf files.

Step 7: Enable Entity Framework Migrations. Open the Package Manage Console. Tools > Nuget Package Manager > Package Manager Console (a Powershell command line

  • At the PM> prompt, type "Enable-Migrations -ContextTypeName  MyFabricStashDb". Press Enter. This will kick off some logic that will enable Code First Migrations. It adds a new folder to the solutions called Migrations. Two files are added to the folder.
  • In Configuration.cs, set AutomaticMigrationsEnabled = true.
  • You can add seed data in the Configuration.cs file as well. Follow the comments in the seed method to see how to do this. Or you can add test data directly to the database by right-clicking on the Fabrics table and "show table data". Add a few entries to see how the listing page looks.
  • If you've added seed data, then use the following command at the PM> prompt,
    Update-database -Verbose
    Now, if you Show Table Data for both tables, you will see the seed data in the database. Note-EF automatically figured out the foreign key, FabricId, and set it in the Purchases table.

Now, onto Working with Data Part 2 and Create an ASP.NET MVC Data-Based Web App Pt 3 >

Add comment

Loading