Learning ADO. NET Entity Framework (1)

Source: Internet
Author: User

Some time ago, many articles in the garden introduced the analysis of LINQ to SQL. It can be said that it is a simple implementation of ORM, And it is Microsoft's problem of unbalanced impedance between data and objects. C #3.0 introduces some new features, such as extension method, Lambda expression, and anonymous type. In fact, these new features are used to serve LINQ. By writing a large number of SQL query statements, values, and assignments in the past, the developer has made a lot of work on intelliisense and compilation, this allows these queries to be checked during compilation. Microsoft also released the ADO. NET Entity Framework, which is the next generation of ADO. net. It is a more powerful ORM than LINQ to SQL. Developers only need to focus on the development of object models in the domain, rather than how they interact with relational databases.

This article describes how to use Entity Framework by using a simple example. Before that, you must download ADO. NET Entity Framework runtime and tools. Many examples are available on the official website. Next we will officially start the ADO. NET Entity Framework Tour (Development Tool vs 2008 beta, taking the northiwind database as an example ).

Create a console Project (select. NET Framework 3.5), click Add new item, and you will see the options of ADO. NET Entity Data Model:

Then, the wizard appears. Select the database, select the table, view, and stored procedure, and follow the wizard step by step. Here, only the customers and orders tables are selected. Click northwind. edmx in the solution entity e view to view the entity information (which looks like the dbml design view of LINQ to SQL), and rename them customer and order.

Now we can query it. First reference the namespace:

using System.Data.Objects;using NorthwindModel;

First, we will query all the customers, and the code is very similar to that in LINQ to SQL.
 using (NorthwindEntities ctx = new NorthwindEntities()) {     foreach (var customer in ctx.Customers)     {         Console.WriteLine(customer.CustomerID);     } }

Query a customer,

 Customer alfki = ctx.Customers.Where("it.CustomerID = 'ALFKI'").First();

"It. customerid = 'alfki' "indicates a query condition statement. The statement looks like a C # code and an SQL statement, and becomes an Entity SQL language, the specific syntax has a detailed reference in the help document.

You can also use LINQ to query (LINQ to entities) and reference the system. LINQ namespace. It seems that Entity Framework does not support the single method and will throw an exception.

 Customer alfki = ctx.Customers.First<Customer>(it => it.CustomerID == "ALFKI");
Or
 Customer alfki = (from c in ctx.Customers                  where c.CustomerID == "ALFKIA"                  select c).First();
It is very easy to perform one-to-many queries in Entity Framework. However, it is worth noting that Entity Framework uses lazy load by default, which means associated data is loaded only when necessary.
In this example, If you directly query the orders associated with the customer, the query results are not found.
 foreach (var order in alfki.Orders) {     Console.WriteLine(order.OrderID); }

You need to call alfki. Orders. Load () before using orders, and run it again to get the correct result.

[To be continued]
 

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.