» Creating a Layered Business Application in Visual Studio
These tutorials are no longer maintained. Some of these tutorials continue to be maintained for the lecture Advanced Concepts in Software Engineering.

This tutorial explains how to implement a business application with several layers in Visual Studio 2012 Ultimate (using SQL Server 2012 Express, see README in library application). It uses ADO.NET Entity Framework and ASP.NET. First, have a look at the library application (.NET) as an example of such an application. Make yourself familiar with Visual Studio and with the structure of the library application in .NET. Then, proceed with this tutorial to create a similar business application from scratch.

In Visual Studio, applications are developed in so called "solutions". A solution contains projects of different types. In this tutorial, two projects will be created:

  1. A class library representing the business layer (data and logic).
  2. A web application.
  3. Remove an old database.

This is just an example. It would also be possible to split the data and logic parts into two projects or to choose a different structure.

I. Creating the Business Layer

  1. Create a C#-"Class Library" by selecting "File -> New -> Project..." from the menu bar. Choose a name for the project ("Name" at the bottom of the window), e.g. "BusinessLayer", and for your application ("Solution").
  2. First, create the data model of your application. In the "Solution Explorer", right-click on the newly created project and select "Add -> New Item...". Select "ADO.NET Entity Data Model" and choose a name. In the following wizard, choose "Empty model" as the initial content of the data model and click "Finish".
  3. Create your data model by dragging elements (entities, associations) from the toolbox onto the designer. If it is not visible, open the "Properties Window" from the "View" menu. To edit the properties of an element, select it in the designer. This approach is called "Model First".
  4. When your data model is complete (at least a first version of it), you have to generate a database schema for it.
    1. Open the "Server Explorer" (under "View" in the menu bar).
    2. Right-click on "Data Connections" and select "Create New SQL Server Database...".
    3. Set "Server name" to "localhost\sqlexpress" and choose a "New database name". Press OK.
    4. Right-click on the white background in the Entity Designer and choose "Generate Database from Model...".
    5. Select the data connection you just created, make sure it will be saved in App.config and click "Next".
    6. Save the generated script to a file of your choice and click "Finish". Your conceptual Entity Data Model is now mapped to a relational schema. However, the tables of this schema have not yet been created in the database.
    7. Open the SQL script from the previous step. Right-click somewhere in the editor and choose "Execute SQL".
    8. When asked to connect to a database, set "Server name" and "Connect to database" (press "Options >>", then switch to the tab "Connection Properties") to the respective values from step 3. Click "Connect". This will create the necessary tables.
  5. The Entity Designer automatically creates C# classes for entities and a container class. Use these classes in your business logic to access the database.

II. Creating the Web Layer

  1. Choose File->New->Project... and create a "ASP.NET Empty Web Application" ("Visual C# > Web"). At the bottom of that window choose a name e.g. "WebLayer" and select "Add to solution".
  2. In the solution explorer, right-click on "References" of your web project and select "Add Reference...". From the tab "Projects", choose your business layer project and click "OK". This reference allows you to access public classes and methods of the business layer.
  3. Copy the connectionStrings section from the App.config file of the business layer project to the Web.config file of the web project. This is necessary as only the top-level configuration is in effect when running the application.
  4. Implement the web application, referencing the business layer where necessary.

III. Remove an old database

  1. By accident you may have deleted a database connection within VisualStudio. There are two ways of deleting an old database:
    1. Using Transact-SQL
      1. Connect to the Database Engine
      2. From the Standard bar, click New Query
      3. Copy and paste the following example into the query window and click Execute. The example removes the LibraryEF database
        USE master ;
        GO
        DROP DATABASE [LibraryEF];
        GO
    2. Using SQLCMD
      1. Open a command-line
      2. Connect to the Database Engine by executing: sqlcmd -S localhost\sqlexpress
      3. Enter drop database [LibraryEF]; and go
      4. Enter exit to leave the SQL command-line