This chapter focuses on how to connect to a database using Entity Framework. Here, we will discuss how to create a web API using ASP.NET and how to consume these web APIs using AngularJS. This chapter will discuss the following topics:
SPA is the latest inclination to develop applications. It loads a single HTML page initially and then dynamically updates the portion of the initially loaded HTML page as the user interacts with the page. An SPA application provides rich experience to the user who will otherwise have to reload the entire HTML page, and also reduces the round trips to the server. To develop an SPA application using the AngularJS framework, which includes features that are provided by Angular and work very well with Entity Framework, a web API is created using Entity Framework and provides standard HTTP verbs. These verbs, such as GET
, POST
, PUT
, and DELETE
are used to perform CRUD operations.
On the client-side, the AngularJS framework also uses the same standard HTTP verbs to communicate with a server through a web API.
ActiveX Data Objects (ADO), used in a .NET framework; ADO.NET is a set of classes, which are used to expose data access services to a .NET framework developer. ADO.NET contains a set of components that create distribution and data-sharing applications. Using ADO.NET, we can create frontend database clients and data access layers for use by applications. An application developer frequently uses ADO.NET to access databases in relational database systems, such as SQL, Oracle, and so on.
ADO.NET is a very powerful framework to use for accessing data. It has been in the market for many years. ADO.NET Entity Framework is an enhancement to traditional ADO.NET, which enables the developer to develop data access applications by using a conceptual data model instead of directly connecting to the relational database. With the Entity Framework, the amount of code and the maintenance required for data-oriented applications significantly decreases as compared to other applications. Microsoft Entity Framework is an Object-relational mapping (ORM), which eliminates the need for most data access code that a developer usually writes. The ADO.NET Entity Framework provides change tracking, identity resolution, lazy loading, and query translation. This way, developers are more focused on applying business logic instead of data access fundamentals.
The ADO.NET Entity Framework consists of the following:
EntityResultSets
or EntitySets
.The following figure shows the structure of the ADO.NET Entity Framework:
The ADO.NET framework Entity Data Model (EDM) is a concept that describes the structure of the data, irrespective of its data store. It is a conceptual representation of the relational database in the form of entities (classes) and the relationships between these entities. It is defined in a Domain Specific Language (DSL), which implements the concepts of EDM. Entities and relationships in EDM allow the developer to focus on the conceptual model instead of the relational database schema.
The ADO.NET framework in Visual Studio initially generates one to one mapping between a relational database and the conceptual data model schema. In the relational database, the data is stored in the form of tables and these tables are related to each other using primary and foreign keys. In the conceptual data model, each property of the entity (class) maps to a certain column of the database table. The entities can be related to each other, independent of the relationship in the physical schema. The ADO.NET Entity Framework uses EDM to perform the mapping and uses ESQL to perform queries, operations, and updates on the entities. It then translates these queries into the native SQL flavor of the underlying database. The ADO.NET Entity Framework provides object services, which represent the relational database tables in place of entities as objects with elements and relationships exposed as an entity's properties. These entities (classes) can be accessed with the help of any object-oriented programming language. We can also consume these entities in client-side applications using web services. For example, Windows Communication Foundation (WCF) data services or XML, which are used when entities are serialized for persistent storage or for over-the-wire transfer.
This figure shows the ADO.NET framework mapping:
The entities in Entity Framework are examples of the types of entities. Each entity represents the instance of the objects. For example, customer information can be retrieved from the customer table in the relational database. The entity type defines the class of an entity and the properties it should have. Properties describe the characteristics of the entity by giving it a name and a type. The properties are fully typed and are compatible with the type systems used in Database Management System (DBMS), and this is the common type system of the .NET framework. The properties of Entity can be SimpleType
, resembling primitive data types, for example, an integer, string, float, and so on; or ComplexType
, which is an aggregate of multiple properties of a simple or complex type.
All entities are accommodated in EntityContainers
, which are unique for each project. Each project can contain one or more EntityContainers
in order to refer to the entities across multiple namespaces and entity types. EntitySets
store multiple instances of one entity type and entity types can have multiple EntitySets
.
Entities can be related either by an association relation or a containment relation. For example, let's consider an entity shipment, which is used to bill a customer. It is an association relation, while another entity order, which contains the order details, is a containment relation. Containment relations are also used for inheritance between entities.
Relationships in the ADO.NET framework also provide operations or actions associated with a relation, which permits us to perform actions on the entity in the event an action is being performed. When some operation is done on a related entity, a relationship can be specified on which an action can be taken. The ADO.NET Entity Framework supports the following types of relationships:
This figure shows the Entity Framework's entity model designer:
The ADO.NET Entity Framework uses a structured query language called Entity SQL, which writes declarative queries, and updates entities and entity relationships at an intangible level. Entity SQL is different from traditional SQL. It does not have explicit constructs to join because EDM is designed for intangible partitioning of data through tables. The EntityClient
classes are used to query against the conceptual model. The query pipeline parses the entity SQL query into a command tree, isolating the query across multiple tables, which are handed over to the EnitityClient
provider.
The ADO.NET Entity Framework is an object relational database mapper, which enables a .NET developer to easily access objects (classes) generated by the Entity Framework of an existing relational database. We will use the Northwind database, which can be downloaded at http://www.telerik.com/forums/where-to-download-northwind-mdf-and-telerik-mdf and Visual Studio 2013 Professional, though any other version of Visual Studio can also be used. This database consists of several tables, such as products, order details, supplier, categories, territories, region, employee territories, employees, orders, customers, shippers, customer demographics, and customer demo. Save this downloaded Northwind database to your computer. The following steps show you how to connect to the Northwind database:
App_Data
folder. Select Add and then select Existing Item…. Browse to the downloaded Northwind database (Northwind.mdf
). We will keep the Northwind database in the project but you can attach it to the SQL server:northwind.mdf
is selected automatically. If you are using the SQL server, click on New Connection and follow the steps to connect to the SQL server: