130 likes | 272 Views
Data Modeling Seminar. February 18 , 2012 Lesson 7 Advanced Uses of Entity Framework. Recap. We’ve brainstormed our design We’ve created a database for our design We’ve normalized the database to optimize it
E N D
Data ModelingSeminar February 18, 2012 Lesson 7 Advanced Uses of Entity Framework
Recap • We’ve brainstormed our design • We’ve created a database for our design • We’ve normalized the database to optimize it • We’ve created a Data Access Object, using Entity Framework, so that we can easily use our database in our application
The Next Step • Now that we’ve got an advanced object that can read/write from/to our database, what else can we do with Entity Framework? • Let’s experiment!
Walkthrough • Let’s make one more application. • Open up Visual Studio 2010.
Walkthrough • From the 'File' menu select 'New' >> 'Project'. • Under Installed Templates, click on the Web category under Visual C#. • In the center panel, select an ASP.NET Dynamic Data Entities Web Application and then click OK.
Walkthrough • Just like in the last lesson, we’re going to let Visual Studio create an Entity Framework model for us. • Right click on the project file and from the context menu, select Add >> New Item. • Under the Data category, select ADO.NET Entity Data Model and then click OK.
Walkthrough • Select Generate from database and click next. • Find the ClassSchedule database we’ve been using and click next. • Make sure to check the boxes to include all Tables, Views, and Stored Procedures and click Finish.
Walkthrough • In the solution explorer, find the Global.asax file and open it • Find the line that looks like this: DefaultModel.RegisterContext(typeof(YourDataContextType), newContextConfiguration() { ScaffoldAllTables = false}); • It is in the function RegisterRoutes • If you have line numbers turn on, look for line 30. • We want the Global.asax file to use the model we just created as the DefaultModel, so uncomment the line and change it to look like this: DefaultModel.RegisterContext(typeof(ClassScheduleEntities), newContextConfiguration() { ScaffoldAllTables = true });
Walkthrough • Now save the Global.asax file and press “Ctrl + F5” to start the project without debugging • Note: By default, debugging is disabled for a Dynamic Data Entities Web Application. • What happens?
Walkthrough • If you set up your Global.asax file correctly, Visual Studio should launch the home page for your Dynamic Data Entities Web Application.
Walkthrough • Visual Studio has created a dynamic website, all with fully functional pages just by looking at our model (which was also created automatically). • In just about 5 minutes, we’ve gone from only having a database, to having a project that can access the database and a website that allows us to view, add, update, and delete data all from the browser.
Walkthrough • It is outside the scope of this seminar, but if you want to take a closer look at how Visual Studio is creating this website on the fly, check out the DynamicData folder in your project. • In it, Visual Studio defines User Controls that can apply broadly to any kind of entity in your model.