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
Build the solution, which generates Entity Framework classes that are based on the tables in your database.
On the View menu, click Object Browser.
In the Browse box, select My Solution.
Note the
Dinner
andMiniNerdDinnerContainer
generated classes as shown below.
Add a controller to the application
In Solution Explorer, right-click the Controllers folder, click Add, and then click Controller.
In the Add Controller dialog, type
DinnersController
and click Add. This adds aDinnersController.cs
file to the Controllers folder. This file has aDinnersController
controller that processes incoming requests for theMiniNerdDinner
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
Add the following member declaration to the
DinnersController
type. TheMiniNerdDinnerContainer
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
.Also add the
FindUpcomingDinners
method toDinnersController
. 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; }
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; } } }
Build the application.
See Also
Concepts
Getting Started with SQL Server Modeling CTP and Visual Studio 2010