Asp. NET no magic--asp.net MVC and database orm

Source: Internet
Author: User

What is ORM

Object Relational Mapping (objects relational Mapping, ORM, or O/RM, or O/R Mapping) is a program technology that enables the conversion of data between different types of systems in an object-oriented programming language. (Baidu Encyclopedia).
Object-relational Mapping (ORM, O/RM, and O/R mapping Tool) in computer science was a programming technique for converting Data between incompatible type systems using object-oriented programming languages. (Wikipedia)
The core points of the above two explanations are "object-oriented", "different type system", "Data Transformation", for applications developed using ASP. C # is an object-oriented programming language, and the defined entity class post is a blood loss model (only attributes have no method) objects. and the database system and the blog system is two different systems, for the database system it regards data as a form of numbers or strings of a table, and the object-oriented language developed by the program to consider data as a single instance, tabular data and instance data is incompatible, so need to convert, such as the previous code:

In the previous code, the conversion was done by writing your own code, since ORM is a program technology, how is it reflected in ASP.

Entity Framework

The Entity Framework (EF) is an object-relational mapper that lets. NET developers can use domain objects to manipulate relational data and help developers omit most of the code that accesses the data.
How do I use the entity Framework?
Before describing how to use EF, let's look at several workflows for EF, as shown in:

  

Model First (Create a new database):
0 Create the model in the designer.
0 Create a database from a model.
0 entity classes are generated automatically from the model.

Database First (using an existing database):
0 Reverse engineering model in the designer.
0 entity classes are generated automatically from the model.

Code First (new database):
0 Define entity classes and mappings (such as mappings for classes and tables, mappings for properties, and table columns) in code.
0 Create a database from a model.
0 Use migrations to update the database (structure).

Code first (using existing database):
0 Define entity classes and mappings (such as mappings for classes and tables, mappings for properties, and table columns) in code.
0 or use reverse engineering to generate entity classes and mappings.

manipulating databases with EF Code first

For this series of articles only code first (* ^_^ *) is introduced
Now the entity class already exists in the blog code and the database and the corresponding data table already exist, so there is no need to create it again.
1. Get the Entity Framework library through NuGet:
Because Blogrepository is the repository for managing data stores, you should install EntityFramework on the project:

    

2. Create the DbContext type:
DbContext is the core type in EF, where the data from the database is populated with entity objects, change tracking, and persisting data to the database are DbContext completed. The most common way to create a dbcontext is to inherit the DbContext type, DbContext type is located under the System.Data.Entity namespace under the EntityFramework assembly:

  

  

3. Add the Dbset attribute to the entity:
The Dbset property represents a collection of data for a specified type, and all additions, deletions, modifications, and queries to the data are implemented through this property.

  

4. Specify the database connection string for DbContext by constructing the method:

  

5. Replace the blogrepository with the Blogcontext operation database:

  

Note: Don't forget to add a LINQ namespace to work with data collections.

6. Run the program (View the blog list page):

  

The error message shows that the ADO provider named "System.Data.SqlClient" could not be found, why?

When you install EF with NuGet, the configuration information is automatically created in Web. config or app. config:

  

In this case, EF is installed on the Blogrepository Class Library project, so the program is not able to read these configuration information at startup, but it is important to note that the error message focuses on "unable to find the ADO Provider" instead of finding the configuration information . So in fact the root cause of this problem is that after installing the EF component, the default is to add EntityFramework.dll and EntityFramework.SqlServer.dll Two library file references, However, because EF is not installed on the startup project, only the assemblies that are dependent on the master project are copied at the time of Project compilation, as well as the assemblies that are used by the dependent assemblies . This means that blogrepository only uses EntityFramework.dll and does not use EntityFramework.SqlServer.dll, so the latter is not in the bin directory of the main project, so it is only necessary to copy the assembly into the bin directory of the MVC project to be shipped Yes:

  

  

  Note: EF is based on the "Contract over configuration" principle, so the default convention for EF is to use SQL SERVER, so if there are no special requirements, there is no configuration information for EF to work properly, and more about the EF configuration will be done in subsequent chapters.

Summary:

This chapter focuses on the ORM Framework Entity Framework in ASP., and demonstrates how to install EF, create DbContext and Dbset, and use them to access the database. After using EF to get the database data code is much less than before, a code can complete the data acquisition function, greatly improve the development efficiency.

One of the issues to note in this article is that EF is indirectly used by the main project, so some EF-related features such as update Database (update-database) and database Migration (migration) The use of commands differs from EF's direct use by the main project, and more about EF will be covered in subsequent articles.

Reference:

https://msdn.microsoft.com/en-us/library/aa937723 (v=vs.113). aspx

https://msdn.microsoft.com/en-us/library/ee712907 (v=vs.113). aspx

This address: http://www.cnblogs.com/selimsong/p/7651602.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC and database orm

Related Article

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.