Create the Data Access Layer

Source: Internet
Author: User
Tags microsoft sql server relational database table

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/ Getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

This tutorial describes what to create, access, and review data from a database using ASP. Web Forms and Entity Framewor K Code first.

This tutorial builds on the previous tutorial "Create the Project" and are part of the the Wingtip Toy Store Tutorial series.

When you've completed this tutorial, you'll have built a group of data-access classes that is in the Models fo Lder of the project.

What are you ' ll learn:
    • How to create the data models.
    • How to initialize and seed the database.
    • How to update and configure the application to the database.
These is the features introduced in the tutorial:
    • Entity Framework Code First
    • LocalDB
    • Data Annotations
Creating the Data Models

The Entity framework is an object-relational mapping (ORM) framework.

It lets the relational data as objects, eliminating most of the data-access code that you ' d usually need to writ E.

Using the Entity Framework, you can issue queries using LINQ, then retrieve and manipulate data as strongly typed objects.

LINQ provides patterns for querying and updating data.

Using Entity Framework allows creating the rest of your application, rather than focusing on the data acce SS Fundamentals.

Later in this tutorial series, we'll show you what to do with the data to populate navigation and product queries.

Entity Framework supports a development paradigm called Codefirst.

Code First lets you define your data models using classes.

A class is a construct this enables you to the create your own custom types by grouping together variables of the other types, met Hods and events.

You can map classes to the existing database or use them to generate a database.

In this tutorial, you'll create the data models by writing data model classes.

Then, you'll let the Entity Framework create the database on the fly from these new classes.

You'll begin by creating the entity classes this define the data models for the Web Forms application.

Then you'll create a context class that manages the entity classes and provides data access to the database.

You'll also create an initializer class so you'll use it to populate the database.

Entity Framework and References

By default, the Entity Framework is included if you create a new ASP. NET Web application using the Web Forms template.

Entity Framework can is installed, uninstalled, and updated as a NuGet package.

This NuGet package includes the following runtime assemblies within your project:

    • Entityframework.dll–all the common runtime code used by Entity Framework
    • Entityframework.sqlserver.dll–the Microsoft SQL Server provider for Entity Framework

Entity Classes

The classes you create to define the schema of the data is called entity classes.

If you ' re new to database design, think of the entity classes as table definitions of a database.

The property is in the class specifies a, column in the table of the database.

These classes provide a lightweight, object-relational interface between object-oriented code and the relational table str Ucture of the database.

In this tutorial, you'll start out by adding simple entity classes representing the schemas for products and categories.

The products class would contain definitions for each product.

The name of each of the members of the product class would be,,,,, ProductID ProductName , and Description ImagePath UnitPrice CategoryID Category .

The category class would contain definitions for each category, a product can belong to, such as Car, Boat, or Plane.

The name of each of the members of the category class would be CategoryID , CategoryName , Description , and Products .

Each product would belong to one of the categories.

These entity classes would be added to the project ' s existing Models folder.

1.In Solution Explorer, right-click the Models folder and then select Add, New Item.

2.Under Visual C # from the installed pane to the left, select Code.

3.Select class from the middle pane and name this new class Product.cs.

4.Click Add.
The new class file is displayed in the editor.

5.Replace The default code with the following code:

6.Create another class by repeating steps 1 through 4, however,

Name the new class Category.cs and replace the default code with the following code:

As previously mentioned, the Category class represents the type of product, the application is designed to sell (such as "Cars", "Boats", "rockets", and so on),

And the Product class represents the individual products (toys) in the database.

Each instance of a Product object would correspond to a row within a relational database table, and all property of the Prod UCT class would map to a column in the relational database table.

Later in this tutorial, you'll review the product data contained in the database.

Data Annotations

Noticed that certain members of the classes has attributes specifying details about the member, such as .

These is data annotations.

The data annotation attributes can describe how to validate user input for this member, to specify formatting for it, and To specify how it was modeled when the database was created.

Context Class

To start using the classes for data access, you must define a context class.

As mentioned previously, the context class manages the entity classes (such as the Product class and the Category Class) and PR Ovides data access to the database. +

This procedure adds a new C # context class to the Models folder.

1.right-click the Models folder and then select Add, New Item.
The ADD New Item dialog box is displayed.

2.Select Class from the middle pane, name it ProductContext.cs and click Add.

3.Replace The default code contained in the class with the following code:

This code adds System.Data.Entity the namespace so and you have access to all the core functionality of the Entity Framework, which includ ES the capability to query, insert, UPDATE, and delete data by working with strongly typed objects.

The ProductContext class represents Entity Framework product database context, which handles fetching, storing, and updating Product class instances in the database.

The ProductContext class derives from the DbContext base class provided by Entity Framework.

Initializer Class

You'll need to run some custom logic to initialize the database the first time of the context is used.

This would allow seed data to being added to the database so, the can immediately display products and categories.

This procedure adds a new C # initializer class to the Models folder. +

1.Create another in the Class Models folder and name it ProductDatabaseInitializer.cs.

2.Replace The default code contained in the class with the following code:

As you can see from the above code, when the database was created and initialized, the property is Seed overridden and set .

When Seed the "is" set, the values from the categories and products were used to populate the database.

If you attempt to update the seed data by modifying the above code after the database have been created, you won ' t see any Updates when you run the WEB application.

The reason is the above code uses a implementation of the DropCreateDatabaseIfModelChanges class to recognize if the model (schema) has changed BEFO Re resetting the seed data.

If no changes Category Product is made to the and entity classes, the database is not being reinitialized with the seed data.

Create the Data Access Layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.