LinQ Learning (III)-Follow-up of LinQ to Entity

Source: Internet
Author: User

Due to the limited time yesterday, there are still some follow-up. To maintain the habit of summing up and writing blogs on a daily basis, I have finished the basics of the LinQ technology today, the rest is to see how we can make the best use of it.

  • Today's highlights:

 

1. Concurrent management mechanism of LinQ to Entity

2. Implementation of LinQ to DataSet

3. EntityDataSource Control

 

 

1. First of all, I drew a sketch of the concurrency mechanism of LinQ to Entity, which can be easily understood Based on the implementation mechanism described in the blog yesterday.

In fact, the technology of LinQ to Entity can come down to one sentence: It is to map the database object to a class, and each row of data in the database is an object of the class, and then all are mapped to the memory, after the LinQ expression is used, the data corresponding to the operation object is loaded into the memory, and the expected data is modified as a temporary backup in the memory, then, the Entity FrameWork is submitted for parsing as the final SQL statement and updated.

However, this leads to a fatal defect:Before executing an update in your LinQ expression, the record in the database is updated due to other operations such as the ADO. NET method. As a result, the memory data you operate on is no longer up-to-date. If SaveChange () is submitted, OptimisticConcurrencyException is thrown.

Let's take a look at a sample code yesterday: update a record

// Replace the inserted customer name [no sub-accounts] with [hahaha] public void updateData () {// instantiate the database object ObjectContext NorthwindEntities myTestDB = new NorthwindEntities (); customer data = (from customer in myTestDB. MERs where customer. customerID = "george" select customer ). single (); // operation memory data. contactName = "HAHAHA"; // assume that ContactName updataNameByADOnet (strSQL) in the database has been updated in other ways below ); // here is a virtual update try {// submit data update myTestDB. saveChanges ();
}
Catch (OptimisticConcurrencyException e){


}}

 

 

 

The data object cannot be locked because only memory resources are involved and the database server cannot be accessed. However, a solution is provided to add a CurrencyMode attribute for each object field mapped to the source code environment to set whether to verify the field concurrency. However, you need to set fields one by one ...... This is undoubtedly a painful process ....

To maintain the consistency between the memory data and the database data, the ObjectContent object provides a Refresh () method to Refresh the memory data or update the database data to the memory data.

Take the previous example as an example. You can add the following two refreshes in the catch body, and then submit and save the refresh.

// Forcibly refresh the database with memory data to ensure consistency with myTestDB. refresh (RefreshMode. clientWins, data); // refresh the memory data of the database to keep the same myTestDB. refresh (RefreshMode. storeWins, data );

 

2. Next, we will briefly introduce LinQ to DataSet.

First, let's look at the next structure:

To implement DataSet access, you need to implement the following two points:

  • DataSet does not have strongly-typed data. To use a single Field value in a LinQ expression, you must call the DataRow. Field <[string] [int]> ("ColName") Extension Method.
  • DataRow and DataTable only implement the weak IEnumerable interface, and also call the forced type conversion of AsEnumerable () such as DataTable. AsEnumerable.

 

3. There is no difference between the EntityDataSource control and sqlDataSourcek control, and a visual database connection model is provided. You do not need to bind a LinQ expression to data in the backend page_load method. The previous functions can be implemented using the following controls.

  • EntityDataSource: connect to the data source

Generate the page script:

<P> the first article on linQ: What I Learned </p> <asp: gridView ID = "GridView1" runat = "server" AutoGenerateColumns = "false" performanceid = "entityperformance1"> <Columns> <asp: boundField DataField = "CustomerID" HeaderText = "Customer ID"/> <asp: BoundField DataField = "ContactName" HeaderText = "customer name"/> <asp: boundField DataField = "City" HeaderText = "City"/> <asp: BoundField DataField = "Country" HeaderText = "Country"/> </Columns> </asp: gridView> <asp: entityDataSource ID = "EntityDataSource1" runat = "server" ConnectionString = "name = NorthwindEntities" defaultinername = "NorthwindEntities" EnableFlattening = "False" EntitySetName = "Customers" Select = "it. [CustomerID], it. [ContactName], it. [City], it. [Country] "> </asp: EntityDataSource>

 

Page effect:

  • QueryExtender: Query and filter.
    <asp:QueryExtender ID="QueryExtender1" runat="server" TargetControlID="EntityDataSource1">      <asp:SearchExpression></asp:SearchExpression>      <asp:RangeExpression></asp:RangeExpression>      <asp:PropertyExpression></asp:PropertyExpression>      <asp:OrderByExpression></asp:OrderByExpression>        </asp:QueryExtender>

 

 

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.