Visual Studio uses EF, linq2sql to quickly create data interaction layers (i)

Source: Internet
Author: User

At the beginning of the project, creating the database interaction layer code is the primary task of the underlying framework. Common practices include code generators such as manual coding, hibernate, or soft-moving, and most people ignore them. NET environment vs offers two very useful data-tier tools.

EF and Linq2sql two sets of frames. In fact they are definitely the quickest and most convenient compared to other ways. For the consumer, whether EF or Linq2sql, they have configured the project with the correct database connection by default, drastically reducing the amount of work in the code.

EF is an abbreviation for the entity Framework and should now be in the 7th version (seemingly in beta version).

Microsoft has given the following definition of the Entity Framework:

The Microsoft ADO Entity Framework is an object/relational Mapping (ORM) framework, the enables developers to work wit H relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that develope Rs usually need to write.

In fact, according to Microsoft's definition, EF is designed to simplify the developer workload and launch the framework. is an example of an EF workflow process.

In fact, EF supports building entity classes from data tables, generating data tables from entity classes, and automatically creating entity classes and data tables from the database design (db Model).

This can significantly reduce the cost of the database design, build tables and realistic body class work directly. After all, that's what the Yards farmers do, how can programmers waste their time.

So far, EF6 should be up-to-date. The following is a brief introduction to the use of EF, starting with the schema of the Domain class, table.

VS has its own integration with EF, the steps to create the data tier for EF are to add a database connection to the solution, then create a new project under the solution as the data interaction layer, and then create a new EF under the project.

1. Establish a database connection.

2. Create an ADO database entity

3. Select Code First, here I have a database already exists.

4. You can see that the results have been created for all selected data table entity models.

Next we write a piece of code for data access. In my example, we plan to query a table with the current amount of data above 1 million records. Let's make a simple select query.

 Public voidTest1_select () {Model1 Modelcontext=NewModel1 (); //Select from Rt_leakprobe    vartemp =modelContext.rt_LeakProbe.toList (); LongCount =temp. Count ();
DateTime lastdate= Temp. GroupBy (x = x.uploadtime). Max (x =X.key); Console.WriteLine ("count is: {0}; Last Data time is: {1};", Count, Lastdate.tostring ("YYYY-MM-DD HH:mm:ss:ffff"));}

At the same time that we created the data table with DB First, we get a class that is named Model1 by default. This class is actually the core of all the data tables we manipulate. As you can see in the previous sheet, Model1 inherits the DbContext class.

As for the DbContext class, it can be understood as a base class with access to the configuration item, where the project we are going to access is the configuration item that can be found in the project's app. Config. (You can also think of it as a database connection)

The above code is tested using the MS Test framework to create unit test forms.

It is important to note that MS test also needs to use NuGet to install EF for normal read to the database connection configuration.

Query results, data volume 1067189, the latest record upload time is 11:54:25.777.

I believe that EF's advantages have been reflected. Of course, individuals temporarily think that there are certain deficiencies, such as the business table design can be used to simplify a part of the operation, regardless of EF, hibernate or linq2sql provide direct mapping function, so to do inheritance is very troublesome.

But in fact the direct mapping in the database design is very reasonable premise, is the simplest and reliable practice.

All in all, the EF tools are very efficient and make it much easier for developers to spend time building the data tier. Individuals also believe that EF is more efficient than hibernate and soft-moving tools.

Visual Studio uses EF, linq2sql to quickly create data interaction layers (i)

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.