Learning ADO. NET Entity Framework (2) [ZT]

Source: Internet
Author: User
Tags connectionstrings
ADO. NET Entity Framework, that 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. The previous article briefly introduced how to use the ADO. Net object framework in the project. From now on, it officially entered the learning journey of ADO. net. This article Article This section describes how to query data in the ADO. Net object framework (taking the northwind database as an example ).

1. query using entitycommand

In the Entity Framework, we can query by entitycommand, which is like sqlcommand In ADO. net. The difference is that sqlcommand uses standard SQL statements to query databases, while entitycommand uses Entity SQL to query entitycontainer. Of course, the final Entity Framework converts Entity SQL into standard SQL statements to query databases.

   Entityconnection Con = New   Entityconnection ( "Name = northwindentities" ); Con. open (); Using (Entitycommand Cmd = New   Entitycommand ( "Select value c from northwindentities. MERs as C" , Con )){ Entitydatareader Reader = cmd. executereader ( Commandbehavior . Sequentialaccess ); While (Reader. Read ()){ Console . Writeline ( "Id [{0}], contacttitle [{1}]" , Reader ["Customerid" ], Reader [ "Contacttitle" ]) ;}}

First, create an entityconnection. It accepts a parameter to indicate which connection string is used in the config file.

<Connectionstrings>
<Add name = "northwindentities" connectionstring = "metadata =. \ northwind. CSDL |. \ northwind. SSDL |. \ northwind. MSL; provider = system. data. sqlclient; provider connection string = & quot; Data Source = localhost \ sqlexpress; initial catalog = northwind; Integrated Security = true & quot; "providername =" system. data. entityclient "/>
</Connectionstrings>

The connection string is different from the connection string used in ADO. net. Metadata: Specify the paths of the. CSDL/. SSDL/. MSL files. Provider: sqlclient, oledb, or ODBC is used. Provider connection string: the connection string used in the past. Providername indicates that entityclient is used.

Then construct entitycommand and read data through entitydatareader. It is worth noting that entitycommand accepts Entity SQL statements instead of standard SQL statements, for more information about Entity SQL syntax, see the help documentation.

2. query using objectquery

The Entity Framework provides a class named objectquery, which allows developers to query through Entity SQL. The query results are provided as objects.

  Using ( Northwindentities CTX = New   Northwindentities ()){ Objectquery < Customer > Query = CTX. createquery < Customer > ( "Northwindentities. MERs" ); Objectresult < Customer > Result = query. Execute ( Mergeoption . Notracking ); Foreach ( Customer C In Result ){ Console . Writeline ( "Id [{0}], contacttitle [{1}]" , C. customerid, C. contacttitle );}}

First, call the createquery method to create the objectquery object (New can also be used here, but the input parameters are different). It accepts the Entity SQL statement as a parameter. Call the execute Method for query. It accepts parameters of the mergeoption Enumeration type, indicating the method for resolving the conflict. The query results are saved in objectresult as objects (here is the customer.

The following code uses new:

 Using(NorthwindentitiesCTX =New Northwindentities()){Objectquery<Customer> Query =New Objectquery<Customer> ("MERs", CTX );Foreach(CustomerCInQuery ){Console. Writeline ("Id [{0}], contacttitle [{1}]", C. customerid, C. contacttitle );}}

3. Ado. NET Entity Framework tool automatically generates entities andCodeTo help developers reduce the workload. In this way, we can simply write it:

 
 Using(NorthwindentitiesCTX =New Northwindentities()){Foreach(CustomerCInCTX. MERs mers ){Console. Writeline ("Id [{0}], contacttitle [{1}]", C. customerid, C. contacttitle );}}

In fact, objectquery is also used for query. Of course, you can add more conditions to the query. in the previous article, it is not described here.

 

It is worth noting that the automatically generated object class has such a method, such as customer:

 Public Static CustomerCreatecustomer (StringCustomerid,StringCompanyName ){CustomerCustomer =New Customer(); Customer. customerid = customerid; customer. companyName = companyName;ReturnCustomer ;}

Instead of providing constructors with parameters, the createcustomer static method is provided to construct the customer instance. This seems to be related to the problem of anemia and congestion in the previous period. Should the object have behavior, anemia or congestion? Although it is only a method, I believe this shows Microsoft's attitude.

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.