Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introducing ASP.NET MVC 4 and Entity Framework Intertech’s Oxygen Blast – November 26, 2013 Davin Mickelson, [email protected] An Intertech Course Intertech About Me… Developed web applications since 1998 with: FrontPage and Visual InterDev, FPSEs, ASP, COM. Perl, ColdFusion <cftag>, ASP.NET: C#, VB.NET, F#. Intertech Consultant/Instructor since 2005. Intertech Courses: Complete ASP.NET 4.5 (5 days). Programming in HTML5 with JavaScript and CSS3 (5 days). Developing ASP.NET MVC 4 Web Applications (5 days). Programming in C# (5 days) Developing Applications Using Entity Framework 4.1 (2 days). I teach .NET, SQL Server BI, TFS, jQuery, MVC, HTML5, etc. Intertech blog: http://www.intertech.com/blog Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 2 New fishing hat doesn’t work! Intertech ASP.NET MVC 4 Agenda, 1 of 3 Purpose/History of MVC 4 Versions/Features of MVC/Visual Studio Download Instructions Controllers ViewBag/ViewData and TempData Models Model Validation Views ASPX Forms and Razor View Engines Routing New MVC 4 Features Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 3 Intertech Entity Framework Agenda, 2 of 3 Purpose/History of EF Versions/Features of EF/Visual Studio Download Instructions MVC and EF Together Three EF Methodologies Database First Model First Code First Querying EF LINQ to Entities Entity SQL EntityClient API Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 4 Intertech MVC & EF Futures, 3 of 3 ASP.NET MVC 5 EF 6 Visual Studio 2013 .NET 4.5.1 Windows 8.1 Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 5 Intertech Three ASP.NET Web App Architectures ASP.NET Forms Been around since .NET 1.0 was released Visual Studio 2002+ ASP.NET Web Pages Single Razor Web files - no code-behind files Visual Studio 2008+ Web Site or WebMatrix (free!) ASP.NET MVC Visual Studio 2008+ to develop these apps Today, we will look this architecture! <DEMO> Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 6 Intertech What is MVC? Model – View – Controller (MVC) Originally developed in 1978 with Smalltalk at Xerox PARC In recent years, MVC has been used as a Web framework MVC is used with Java and Ruby Jakarta Struts, Ruby on Rails, JSF, Spring, and more “Separation of Concerns” The model, the view, and the controller are broken into separate domains These domains may or may not be defined in separate methods, classes, or assemblies There is no hard set of rules but rather conventions Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 7 Intertech Introducing ASP.NET MVC Popular new way to develop ASP.NET Web applications. MVC applied to the ASP.NET framework. Already been done before with ALT.NET! Example: Castle MonoRail http://www.castleproject.org/projects/monorail/ Minneapolis ALT.NET UG: http://mplsalt.net/MainPage.ashx Microsoft’s release ASP.NET MVC has more pull, though. Based on file and folder naming conventions. High quality coding promoted by unit testing. Test Driven Development (TDD) is supported. Support for DI / IoC frameworks. Ninject, StructureMap, MEF, Castle Windsor, Spring.NET, Autofac, Unity, etc. Simplifies testing parts of an app by using mock objects. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 8 Intertech Why Use ASP.NET MVC? Hate the resource wastefulness of standard ASP.NET forms. Need unit testing. Prefer greater control over the HTML, CSS, and JavaScript sent to the Web browser. Want refined URL addresses for better Search Engine Optimization (SEO). Have previous experience with the MVC model in another language. Tired of standard Web forms and want to try something new and cool! Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 9 Intertech Why NOT Use ASP.NET MVC? Already supporting a large ASP.NET Web forms project that doesn’t need this architecture. Prefer using the ASP.NET server controls. Prefer ASP.NET Rapid Application Development (RAD) environment (drag and drop). Not a professional web developer. Prefer the simplicity of Web pages in WebMatrix. Prefer controls having automatic state management (ViewState). Don’t need this level of control over the final HTML, CSS, and JS. Haven’t had time to sharpen your C#/VB programming skills with: HTML/CSS/JS, Lambda operator, LINQ, Extension methods, Entity Framework, etc. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 10 Intertech ASP.NET MVC vs. ASP.NET Forms ASP.NET Web Forms ASP.NET MVC We know this stuff already! Entirely new model we must learn Supports ASP.NET Server Controls Primarily supports raw HTML/JS Automatic state management of controls Manual state management Drag and drop controls “Scaffolded” (wizard) views and manual Web page coding Been around since 2002 1.0 released on April 9th, 2009 Weak or custom unit testing required Encourages and includes unit testing support! Web Sites and Web Application Project (WAP) WAP only! Both ASP.NET frameworks are supported in VS 2008/2010/2012/2013+ Both have access to the five intrinsic objects (Session, Application, etc.) Both support the use of the ASP.NET Provider models (Membership, Profile, Sitemap, and more) Both support EF and the popular JavaScript libraries: Modernizr, jQuery, etc. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 11 Intertech ASP.NET MVC - Five Versions Visual Studio ASP.NET MVC Visual Studio 2008 v1/v2 (both added to Visual Studio) Visual Studio 2010 v2/v3/v4 (v2 native), v3 and v4 added Visual Studio 2012 v3/v4/v5 (3.0 native), v4 and v5 added Studio 2013 v4/v5 (both native) Visual ASP.NET MVC is a revolutionary step for Microsoft This is the first time Microsoft has ever embraced the open source model at this level The complete ASP.NET MVC source code can be downloaded from CodePlex, modified, and then recompiled for your needs! http://aspnetwebstack.codeplex.com/ Many free JavaScript libraries included such as jQuery, Modernizr, and Knockout Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 12 Intertech Create an ASP.NET MVC Project Pick a language (C# or VB) for your WAP Click File | New | Project | ASP.NET MVC 4 Web Application Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 13 Intertech Create an ASP.NET MVC Project Choose a template, decide on a view engine, and select whether to create a unit test project Visual Studio supports alternative (installed) testing frameworks: NUnit, MBUnit, and more Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 14 Intertech Project Created! There is a huge number of referenced assemblies! Talk about embracing the open source community! Some are referenced .NET Framework assemblies – System.<anything> Many are NuGet packages installed with the selected project template They are installed in the \packages\ subfolder They are also copied to the WAP’s \bin\ folder Curious what they do? ILDasm.exe! <DEMO> Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 15 Intertech Models, Views, and Controllers These three folders suggest where to define the three domains Conventions: Each controller class must be in the Controllers folder Each controller class contain one or more action methods Each view file is placed in subfolder named after a controller class. Each view file maps to an action method of a controller class Model classes can be created anywhere - the Models folder is fine or in a separate .dll. The Shared folder contains shared resources such as: Web pages, layouts, error view, and partial views. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 16 Intertech Controllers - Purpose A Controller is responsible for handling incoming requests and sending back the proper response. It is the first of the three MVC members to interact with the request. A Controller class contains “action methods” that usually map to views with the same name. Most typically, they interact with a Model to generate or retrieve data and/or use business rules. They take the retrieved data and pass it off to a View for generating a Web response to the user. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 17 Intertech Controllers - Defining A Controller must implement IController interface and must end in the name Controller. Usually, it will inherit from the Controller base class. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 18 Intertech Controllers - Defining Alternatively, the class can implement the IController interface This is a really simple controller that just returns a simple message. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 19 Intertech Controllers - Actions The simplest controller just returns HTML to the Web browser IMPORTANT: By default, all public methods in a controller class can be called from the Web browser Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 20 Intertech Controllers - ActionResult Most controller methods return a type of ActionResult ActionResult Types Purpose Examples of Use ViewResult Displays a View return View(); PartialViewResult Displays a Partial View return PartialView(); RedirectToRouteResult Redirects to a route return RedirectToRoute(“NamedRoute”); RedirectResult Simple URL redirection return Redirect(“http://bing.com”); ContentResult Returns raw text to the browser return Content(rssString, “application/rss+xml”); FileResult Returns binary data to browser return File(@”.\Doc1.docx”, “application/vnd.ms-word”); JsonResult Returns an object serialized as Json return Json(Car); JavaScriptResult Returns JavaScript intended to run on the browser. Used with Ajax. return JavaScript(“$(#elementName).hide();”); HttpUnauthorizedResult Returns code 401 – not authorized return new HttpUnauthorizedResult(); EmptyResult Doesn’t return. Ends. Return new EmptyResult(); Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 21 Intertech Controllers – Passing Data Data is passed to a View using the ViewData or the TempData dictionary collections. They store data as System.Object. ViewData is a ViewDataDictionary object Used for a single Request/Response. ViewBag, introduced with MVC 3, is usually preferred and uses ViewData object. Stores data as “dynamic”,not Object - no casting required in View! TempData is a TempDataDictionary object. Used to preserve data between a RedirectResult or RedirectToRouteResult. Stores data for the current and next HTTP Request/Response. Uses Session object behind the scenes. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 22 Intertech Controllers – Passing Data Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 23 Intertech Controllers – Passing Data The View and the final response displayed in IE 10. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 24 Intertech Controllers - Adding New Controllers can be easily added to the project. There are many (hidden?) context menus available for launching powerful MVC wizards. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 25 Intertech Controllers - Adding Incredible scaffolding support is included! Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 26 Intertech Controllers - Ajax Ajax calls can also be made of the controller. These are also public methods exposed from the controller class. The example below uses the ASPX Forms view engine. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 27 Intertech Models – Purpose Business Logic (BL), Data Access Logic (DAL) – it’s all defined here! Where you actually write the code is up to you. With the Model, you have the most flexibility as to where you’d like to place the code The Model code can be located: Right inside the Controller class (not recommended) In classes defined in the Models folder In classes defined elsewhere in the WAP In a Class Library assembly for greater reuse Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 28 Intertech Models - Purpose A Model is code that enforces business logic. It can also store and return data in a fashion that can ultimately be bound to and displayed by a View There are many different types of models! Specifically, a model can be used for: Retrieving/Storing Data in an .xml file Retrieving/Storing Data in a database Pure business logic A model represents the intelligence of the system Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 29 Intertech Models - Types Models can be based on classic ADO.NET methods defined in CRUD methods of a DAL Often times referred to as POCOs (Plain Old CLR Objects) Models can also be based on Object-Relational Mapper (ORM) software. Examples include: LINQ to SQL (Dead technology - avoid!) Entity Framework (More coming up!) NHibernate (a .NET version of Java’s Hibernate) Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 30 Intertech Views - Description Two different View Engines ASPX Forms View Engine Razor View Engine (MVC 3+) They are never directly requested by the Web browser They are often used with .master pages/_layouts ASPX Views make use code nugget <%: %> syntax This is the same as typing out Response.Write() but shorter Razor uses a simplified code nugget @ syntax based on Web Pages Transitioning from ASP.NET Forms to ASP.NET MVC? Consider initially learning MVC with ASPX View Engine Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 31 Intertech Views - Adding Views give us full control to the HTML and JavaScript mark up Views can be easily added to the project in the Solution Explorer IMPORTANT: Make sure you are adding it to subfolder! Best way to add view? Right-click the code in an action method and click Add New… Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 32 Intertech Views – HTML Helper Methods Views also make use of the HTML Helper methods For example: @Html.TextBox("txtName", ViewData["Name"]) HTML Helper Example Use CheckBox Html.CheckBox(“chkChoose”, true) Radio Button Html.RadioButton(“rbRadio”, “val”, false) Hidden Form Field Html.Hidden(“hField”, “CustomerID”) Password Textbox Html.Password(“pPassword”, “val”) Text Area Html.TextArea(“taTextArea”, “val”, 4, 10, null) TextBox Html.TextBox(“txtName”, “Davin”) Drop-Down List Html.DropDownList(“ddlColors”, new SelectList( new [] {“Red”, “Green”, “Blue”}), “Pick One”)) ListBox Html.ListBox(“lbColors”, new MultiSelectList( new [] {“Red”, “Green”, “Blue”})) Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 33 Intertech Views – HTML Helper Methods Different Types of Helper methods Classic un-typed Helper methods (v1/v2 – no editor help) Strongly-typed Helper methods (IntelliSense!) Template Helper methods (Attempts full object code gen) Rich Helper methods (Chart, WebGrid) Your custom Helpers via Extension Methods <DEMO> Many 3rd Party Helper methods (Fill in the gaps!) Telerik, ComponentSource, CodePlex, GitHub, etc. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 34 Intertech System.Web.Routing.dll Installed with VS 2008 SP1 (.NET 3.5 SP1) and newer Used by Dynamic Data Web applications for routing web requests Also used with ASP.NET MVC for routing requests to the proper controllers Routing is configured in the /App_Start/RouteConfig.cs file via a call from Global.asax. A static method called RegisterRoutes() registers URL requests to controller action methods. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 35 Intertech RouteConfig.cs – Default Route Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 36 Intertech Default Route “Default” is the optional name of the route {controller} is the name of the controller class {action} is the name of the public action method in the controller class {id} is an optional value passed to the action method as a parameter If any or all are not specified, the default URL values are used (http://server:<port #>/Home/Index) Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 37 Intertech Routes can be confusing! More routes can be added They should be added from most specific to most generic The first route that matches a request wins Not necessarily the best match! Important points: The static RegisterRoute() method can be unit tested Routing is a silent but powerful fourth player in ASP.NET MVC You can also define “catchall” wildcard routes for handling all kinds of data tacked onto the end of the query string Consider developing or downloading a route debugger There are free ones available Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 38 Intertech Sample Routes (Ignore port 6831) http://localhost:6831/ http://localhost:6831/product http://localhost:6831/6 http://localhost:6831/product/junk http://localhost:6831/product/Index Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 39 Intertech Route Debuggers Check out some of the route visualizers/debuggers available. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 40 Intertech Unit Testing ASP.NET MVC Good Unit Testing frameworks test all behavior, no matter the visibility declarations They can test private, protected, internal, protected internal, and public members Many unit testing frameworks exist including Visual Studio Testing Framework, NUnit, xUnit.net, and more. http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#.NET_programming_languages Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 41 Intertech Unit Testing ASP.NET MVC Testing the RegisterRoutes() will ensure that no one has changed them Testing the controllers can simulate user requests Web users will try to pass you anything; be ready for that! Testing the Models guarantee correct results Especially important if the models are in separate Class Library assembly for reuse across projects! Testing ORM classes probably unnecessary (opinion!) Testing Views? Maybe Unfortunately, if you change the HTML/JS out just slightly, your View tests will fail Some developer prefer not to tests their Views, or only certain Views Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 42 Intertech Test-Driven Development (TDD) Visual Studio Class Designer supports TDD by making all new methods fail unit tests until they’ve been implemented TDD is optional True TDD developers completely write the tests and then write the code in the methods/properties to pass those tests It is a form of Extreme Programming (XP) Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 43 Intertech New ASP.NET MVC 4 Features New project template wizard. Better scaffolding of action methods and views. Improved “scaffolded” views that support mobile devices via CSS media queries. Support for Web API project template supporting RESTful services with OData queries. Support for OpenID and OAuth authentication including 3rd party sites such as Facebook and Yahoo. Single Page App project template. Mobile project template that include jQuery Mobile. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 44 Intertech Introducing Entity Framework (EF) The ADO.NET Entity Framework (or simply EF) was introduced with Visual Studio 2008 Service Pack 1. .NET 4.0 / Visual Studio 2010 support new EF designers, and a better programming experience. EF 1 left a lot of “opportunities for improvement”. So much so that the next version was EF 4 with .NET 4.0. The version EF 5 switched to Open Source! You can download it as NuGet package. EF 6 was released with VS 2013. Learn more here: http://msdn.microsoft.com/en-us/data/aa937723 Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 45 Intertech Introducing Entity Framework (EF) EF is a programming model which attempts to lessen the gap between database constructs and object oriented programming constructs. Using EF, you can interact with a relational database without ever seeing a line of SQL code (if you so choose). Rather, when you apply LINQ queries to your strongly typed classes, the EF runtime will generate proper SQL statements on your behalf. By way of another example, rather than updating database data by finding a row, updating the row, and sending the row back for processing using a batch of SQL queries, you can simply change properties on an object and save its state. The EF runtime will update the database automatically. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 46 Intertech Introducing Entity Framework (EF) As far as Microsoft is concerned, the ADO.NET Entity Framework is a new member of the data access family not meant to replace the connected or disconnected layers. However, EF works really nicely with MVC. It saves you an incredible amount of typing over the more primitive world of SQL queries and row/column collections. .NET 4.0+ ships with EF providers for Microsoft SQL Server databases. Many third party databases (Oracle, MySQL, etc.) provide EFaware data providers. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 47 Intertech Introducing Entity Framework (EF) The strongly typed classes used within EF are termed entities. Entities are a conceptual model of a physical database which maps to your business domain. Formally speaking, this model is termed Entity Data Model (EDM). While the EDM is a client side set of classes which map to a physical database, understand that the entities need not map directly to the database schema in so far as naming conventions go. Your entity classes can be restructured to fit your needs, and the EF runtime will map your unique names to the correct database schema. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 48 Intertech EF Example For example, assume you have the following physical database table in a SQL Server database: If you were to generate an EDM for the Inventory table of the AutoLot database, the entity will be called (by default) Inventory. However, you could rename this class to Car and rename the properties which map to the columns of the Inventory table. Because of this loose coupling, the entities can be shaped to closely model your business domain. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 49 Intertech EF Example If you were to generate an EDM for the Inventory table of the AutoLot database, the entity will be called (by default) Inventory. However, you could rename this class to Car and rename the properties which map to the columns of the Inventory table. Because of this loose coupling, the entities can be shaped to closely model your business domain. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 50 Intertech EF Example Consider the following Program class, which will add a new row to the Inventory table of AutoLot, using the Car entity class, and a related class named AutoLotEntities. This class is called an object context, whose job it is to communicate with the physical database on your behalf. class Program { static void Main(string[] args) { // Connection string automatically read from generated config file. using (AutoLotEntities context = new AutoLotEntities()) { // Add a new record to Inventory table, using our entity. context.Cars.AddObject(new Car() { AutoIDNumber = 987, CarColor = "Black", MakeOfCar = "Pinto", NicknameOfCar = "Pete" }); context.SaveChanges(); // Save changes back to database! } } } © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 51 Copyright Intertech ObjectContext Members Member of ObjectContext Meaning in Life AcceptAllChanges() Accepts all changes made to entity objects within the object context. AddObject() Adds an object to the object context. DeleteObject() Marks an object for deletion. Must still SaveChanges(). ExecuteFunction() Executes a stored procedure in the database. ExecuteStoreCommand() Allows you to directly send a SQL command to the data store. GetObjectByKey() Locates an object within the object context by its key. SaveChanges() Saves all updates to the data store. CommandTimeout This property gets or sets the timeout value, in seconds, for all object context operations. Connection This property returns the connection string used by the current object context. SavingChanges This event fires when the object context is saving changes to the data store. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 52 Intertech Three Ways to Use EF Database First A model and ObjectContext class is automatically built based on an existing database. Model First A model is built first, then EF automatically creates the database and the ObjectContext class Code First Classes are created by the developer that inherit from DBContext. EF creates the underlying database, typically a LocalDB instance of SQL Express. No .edmx model is created – the classes are the model! Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 53 Intertech Three Layers (Database/Model First) All three layers of the Entity Data Model (EDM) are defined in the *.edmx file. Conceptual Layer This represents the entities and their relationships in the model Physical Layer This represents the underlying tables in the database Mappings This represents the mapping from model to database. Most settings (not all!) can be made using the designer. Each new version of EF released adds more functionality to the designer. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 54 Intertech Database First Most EF applications original started out this way before EF 4.1 May be the most likely way you use EF Oldest way of using EF Greatest support in Visual Studio Model validation more difficult Must use “Buddy classes” so data annotations/validation rules are not lost. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 55 Intertech Model First A very flexible pattern Allows you to design the model using prototypes in the Toolbox. Will use LocalDB database until you change connection string in Web.config Still using Buddy classes for model validation Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 56 Intertech Code First Most flexible to create and easiest for programmers. No visible .edmx designer file created Database created in /App_Data/ using EF base classes DBContext represents the model DBSet represents an entity Database can be recreated: Every time the web app is started When the model changes Data is “seeded” (populated) during application startup in Global.asax Override Seed() method <MVCMusicStore Demo> Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 57 Intertech Querying Entity Framework – 3 ways LINQ to Entities Most commonly, you will use LINQ Query Expressions to retrieve data from EF. Check out the free LINQ editor, LINQPad. Entity SQL Similar to the standard SQL language, this alternate SQL language can be used to query EF. SELECT VALUE c FROM AWLTEntities.Customers AS c WHERE c.FirstName = ‘Jim’ EntityClient API Programming can also be done directly against the EntityClient API. Similar to classic ADO.NET but objects are Entity versions. EntityConnection, EntityCommand, EntityDataReader, etc. Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 58 Intertech Microsoft Tool Futures ASP.NET MVC 5 Is included with VS 2013 next month, Available for VS 2012 now! Includes ASP.NET Identity,Twitter Bootstrap in the MVC template, Authentication Filters, Filter overrides, more… EF 6 Asynchronous data access, Better Code-First Support, more… Included with VS 2013 Visual Studio 2013, looks a little different but easier to use than VS 2012 RTM’d in August, 2013 Support added for Windows 8.1, EF6, and MVC 5 Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 59 Intertech Microsoft Tool Futures .NET 4.5.1 Not that many new features – .NET 4.5 just came out! Suggested moving to NuGet package release Obvious support added for Windows 8.1 Store front apps Windows 8.1 New Start button offering shutdown option Three apps simultaneously can run on Tile view Store front apps are fun to build with C#, VB, or JavaScript. All your JavaScript skills transfer over check it out! NOTE: Must develop on Windows 8/8.1 (not Windows 7 or Server 2012) Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 60 Intertech Thank you! Question?!? Davin Mickelson [email protected] Shoot me an email! Copyright © Intertech, Inc. 2013 • www.Intertech.com • 800-866-9884 • Slide 61