Develop Using Generated Entity Framework Classes

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

This is the fourth step in the Getting Started with SQL Server Modeling CTP and Visual Studio 2010 tutorial. The preceding step is Deploy Domains to the Database. The following step is Add a Main Web Page to the Application. In this topic you will build and use C# data access classes to query and update the database.

Generate Entity Framework classes

  1. Build the solution, which generates Entity Framework classes that are based on the tables in your database.

  2. On the View menu, click Object Browser.

  3. In the Browse box, select My Solution.

  4. Note the Dinner and MiniNerdDinnerContainer generated classes as shown below.

    Shows the generated classes in the Object Browser.

Add a controller to the application

  1. In Solution Explorer, right-click the Controllers folder, click Add, and then click Controller.

  2. In the Add Controller dialog, type DinnersController and click Add. This adds a DinnersController.cs file to the Controllers folder. This file has a DinnersController controller that processes incoming requests for the MiniNerdDinner application.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    
    namespace MiniNerdDinner.Controllers
    {
        public class DinnersController : Controller
        {
            //
            // GET: /Dinners/
    
            public ActionResult Index()
            {
                return View();
            }
    
        }
    }
    

Develop by using the generated classes

  1. Add the following member declaration to the DinnersController type. The MiniNerdDinnerContainer class is automatically generated from the “M” source code and contains the Entity Framework data access logic to interact with the database.

    MiniNerdDinnerContainer container = new MiniNerdDinnerContainer(@"Data Source=.\SQL2008EXPRESS;Initial Catalog=NerdDinner;Integrated Security=True");
    

    Note

    This is the same connection string that was configured in the M Deployment properties page. In this case, the SQL Server 2008 is an Express edition and the instance is named SQL2008EXPRESS.

  2. Also add the FindUpcomingDinners method to DinnersController. This method uses a LINQ query over the generated data access classes to construct a list of upcoming dinners. A subsequent step uses this logic to build the main view for our application.

            private IQueryable<Dinner> FindUpcomingDinners()
            {
                return from dinner in container.Dinners
                       where dinner.EventDate > DateTime.Now
                       orderby dinner.EventDate
                       select dinner;
            }
    
  3. The final code should look as follows (depending upon your particular connection string):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    
    namespace MiniNerdDinner.Controllers
    {
        public class DinnersController : Controller
        {
            MiniNerdDinnerContainer container 
                = new MiniNerdDinnerContainer(@"Data Source=.\SQL2008EXPRESS;Initial Catalog=NerdDinner;Integrated Security=True");
            //
            // GET: /Dinners/
    
            public ActionResult Index()
            {
                return View();
            }
    
            private IQueryable<Dinner> FindUpcomingDinners()
            {
                return from dinner in container.Dinners
                       where dinner.EventDate > DateTime.Now
                       orderby dinner.EventDate
                       select dinner;
            }
    
    
        }
    }
    
  4. Build the application.

See Also

Concepts

Getting Started with SQL Server Modeling CTP and Visual Studio 2010