Entity Framework Database Connection with ASP Notes from https://www.asp.net/mvc/overview/getting- started/getting-started-with-ef-using-mvc/creating-an-

Slides:



Advertisements
Similar presentations
ASP.NET Ajax Supplementary Tutorial. Why Use ASP.NET AJAX? - I ASP.NET AJAX enables you to build rich Web applications that have many advantages over.
Advertisements

Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Presentation Heading – font Arial
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module J (Office 2010 Version) Implementing.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module J (Office 2010 Version) Implementing.
Intermediate Visual Basic CISP 371 CRC Prof. Chapman.
BIM211 – Visual Programming Database Operations 1.
Michael Donovan, River Campus Libraries – 12/03 DocuShare Overview and Training.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
1 of 6 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
Part 05 – Code First Migrations and Azure Deployment Entity Framework and MVC Series Tom Perkins NTPCUG.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
1 of 5 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2006 Microsoft Corporation.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Chapter Extension 6 Using Microsoft Access © 2008 Pearson Prentice Hall, Experiencing MIS, David Kroenke.
1 Chapter 20 — Creating Web Projects Microsoft Visual Basic.NET, Introduction to Programming.
Create a New Application and Project Open the Create Application dialog. Enter the application name of your choice and the directory. Select No Template.
® IBM Software Group © 2006 IBM Corporation The Eclipse Data Perspective and Database Explorer This section describes how to use the Eclipse Data Perspective,
Working with SharePoint Document Libraries. What are document libraries? Document libraries are collections of files that you can share with team members.
An Introduction to ASP.NET Web Pages 2 Module 1: Webmatrix Installation and Your First Web Site Tom Perkins.
Access Tutorial 10 Automating Tasks with Macros
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom.
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Deployment of web Site. Preparing the web site for deployment you now have two versions of web site 1 -one running in the production environment 2-one.
Databases and LINQ Visual Basic 2010 How to Program 1.
CPSC 203 Introduction to Computers T59 & T64 By Jie (Jeff) Gao.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Lesson No:9 MS-Word Tools, Mail Merge and working with Tables CHBT-01 Basic Micro process & Computer Operation.
ASP.NET Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital.
E-Commerce: Introduction to Web Development 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics What is a Web.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Part 04 – Preparing to Deploy to the Cloud Entity Framework and MVC Series Tom Perkins NTPCUG.
2. Introduction to the Visual Studio.NET IDE. Chapter Outline Overview of the Visual Studio.NET IDE Overview of the Visual Studio.NET IDE Menu Bar and.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Course ILT Access basics Unit objectives Define database and database-related terminology, and plan a database Start Access and open, create, view, and.
Introduction In The Name Of Allah, The Beneficent, The Merciful.
Introduction to ASP.NET T.Ahlam Algharasi. The Visual Studio IDE Start page 2.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Slide 1 ASP Authentication There are basically three authentication modes Windows Passport Forms There are others through WCF You choose an authentication.
BIT 285: ( Web) Application Programming Lecture 15: Tuesday, February 24, 2015 Microsoft Azure Instructor: Craig Duckett.
1 Working with ASP.NET Master Pages By huda AlSuwailem Reference: Mastering Microsoft Visual Web Developer 2005 Express Edition by John Paul Mueller.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module J (Office 2007 Version) Implementing.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Introduction to SQL Server  Working with MS SQL Server and SQL Server Management Studio.
0 Copyright 2012 FUJITSU Interstage BOP SQL Query Tutorial Todd Palmer October 2012.
Customizing WebLink Lab 208 Alex Huang. Table of Contents Introduction – What is WebLink Disclaimer Customization Basics and Exercises Resources Questions.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
C Copyright © 2009, Oracle. All rights reserved. Using SQL Developer.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Murach's ASP.NET 4.5/C#, C2© 2013, Mike Murach & Associates, Inc.Slide 1.
Xamarin Development with
Creating Data Base & Sql Data Source
Visual Basic 2010 How to Program
Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek
02 | Developing ASP.NET MVC 4 Models
Principles of report writing
Creating Data Base & Sql Data Source
Implementing Entity Framework with MVC Jump Start
SOAP web services in Microsoft Azure
Security - Forms Authentication
Presentation transcript:

Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an- entity-framework-data-model-for-an-asp-net-mvc- application

Development Models

Database First – If you already have a database, the Entity Framework designer built into Visual Studio can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns.

Model First – If you don't have a database yet, you can begin by creating a model in an.edmx file by using the Entity Framework graphical designer in Visual Studio. When the model is finished, the Entity Framework designer can generate DDL (data definition language) statements to create the database. As in Database First, the.edmx file stores model and mapping information

Code First – Whether you have an existing database or not, you can use the Entity Framework without using the designer or an.edmx file. If you don't have a database, you can code your own classes and properties that correspond to tables and columns. If you do have a database, Entity Framework tools can generate the classes and properties that correspond to existing tables and columns.

The Contoso University Web Application The application you'll be building in these tutorials is a simple university web site. Users can view and update student, course, and instructor information. Here are a few of the screens you'll create. The UI style of this site has been kept close to what's generated by the built-in templates, so that the tutorial can focus mainly on how to use the Entity Framework.

Create an MVC Web Application Open Visual Studio and create a new C# Web project named "ContosoUniversity".

In the New ASP.NET Project dialog box select the MVC template. If the Host in the cloud check box in the Microsoft Azure section is selected, clear it. Click Change Authentication. In the Change Authentication dialog box, select No Authentication, and then click OK. For this tutorial you won't be requiring users to log on or restricting access based on who's logged on.

No Authentication

Styles and Layout changes Open Views\Shared\_Layout.cshtml, and make the following changes: Change each occurrence of "My ASP.NET Application" and "Application name" to "Contoso University". Add menu entries for Students, Courses, Instructors, and Departments, and delete the Contact entry.

Changes

Index.cshtml changes In Views\Home\Index.cshtml, replace the contents of the file with the following code

Installing Entity Framework If it is not installed, use NuGet to install the package. From the Tools menu click NuGet Package Manager and then click Package Manager Console. In the Package Manager Console window enter the following command: Install-Package EntityFramework

Create the Data Model Next you'll create entity classes for the Contoso University application. You'll start with the following three entities:

Creating Classes for Entities The Student Entity In the Models folder, create a class file named Student.cs

The Enrolment Entity The StudentID property is a foreign key, and the corresponding navigation property is Student.

The Course Entity The Enrollments property is a navigation property. A Course entity can be related to any number of Enrollment entities.

Create the Database Context The main class that coordinates Entity Framework functionality for a given data model is the database context class. In this project, the class is named SchoolContext.

Where to put SchoolContext? Add a new folder to the solution and name it DAL (Data Access Layer) Save SchoolContex.cs in the DAL folder

Specifying entity sets Above code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.

Specifying the connection string The name of the connection string (which you'll add to the Web.config file later) is passed in to the constructor. public SchoolContext() : base ("SchoolContext") { }

One thing to note Specifying singular table names The modelBuilder.Conventions.Remove stateme nt in the OnModelCreating method prevents table names from being pluralized. If you didn't do this, the generated tables in the database would be named Students, Courses, and Enrollments. Instead, the table names will be Student, Course, and Enrollment.

Database seeding Set up EF to initialize the database with test data The Entity Framework can automatically create(or drop and re-create) a database for you when the application runs. You can specify that this should be done every time your application runs or only when the model is out of sync with the existing database. The default behavior is to create a database only if it doesn't exist

Seed Method The Seed method takes the database context object as an input parameter, and the code in the method uses that object to add new entities to the database. For each entity type, the code creates a collection of new entities, adds them to the appropriate DbSet property, and then saves the changes to the database.

To tell Entity Framework to use your initializer class, add an element to the entityFramework element in the application Web.config file

Set up EF to use a SQL Server Express LocalDB database LocalDB is a lightweight version of the SQL Server Express Database Engine. You can put LocalDB database files in the App_Data folder of a web project if you want to be able to copy the database with the project. File extension is.mdf LocalDB in particular is not recommended for production use with a web application because it is not designed to work with IIS.

Save in App_Data If you wanted the database to be created in your App_Data folder, you could addAttachDBFilename=|DataDirectory|\Pera.mdf to the connection string.