Test the ADO. NET Entity Framework-shadow

Source: Internet
Author: User

 

 

Preparations:

  1. Upgrade Visual Studio 2008 and. NET Framework 3.5 to SP1. Click to go to the upgrade address.
  2. Install SQL server 2005, and use the EXPRESS version of SQL SERVER in VS 2008.
  3. Download and append the database: Click to download DemoDb.
  4. Create a VB Application. Note that the target Framework must be set to version 3.5.

 

Query instances:

 

1. Create a ing

Right-click the project and add a new project. Select the new "ADO. NET Entity Data Model" and name it NotebookDb. edmx.

Click "add". The Object Data Model Wizard is displayed. In this case, select "generate from database" and set a name for the object data model (DemoDbEntities is used as the default name in the example) after the corresponding data connection is established ), click "Next ".

Select a table. Now we have selected both tables. Click "finish ".

In this way, a seemingly LinQ to SQL Entity Data Model Designer is present.

We can see that the two tables are mapped to two entities, which have two types of attributes: "Scalar Property" and "Navigation Property, it also forms a one-to-many relationship. If you have read a little UML, you will not feel unfamiliar with such relational symbols. Under Visual Studio, the ing details panel is automatically enabled. When we select an object, it automatically displays the ing between the object and the table columns. Column to attribute, database type to. NET type ...... Clear at a glance.

 

2. Modify the ing as needed

Because the Entity Data model exists on the client, its modification will not affect the database. That is to say, we can modify the ing relationship based on the needs of the application, and this modification will not affect the database. This feature will benefit from the use of shared data storage in different fields of the same enterprise. Let's try to delete the Weight ing. It's really easy. Right-click the object and click "delete". Check the ing details panel: Weight has been deleted.

3. Use LINQ to Entity to query and output results

Finally, let's try how to output the query result through LinQ to Entity. In any case, if the value of LinQ is LinQ, The LinQ query cannot be separated from the three steps we mentioned in the new feature of Visual Basic 9.0, LINQ (1:

  • Establish data sources
  • Create Query
  • Execute Query

Our code also corresponds to these three steps:

Sub Main ()

'Create Entity Set

Dim db As New DemoDbEntities ()

 

'Create Query

Dim query = From aLaptop In db. NotebookStorage _

Select aLaptop

 

'Execute Query

For Each aLaptop In query

Console. WriteLine (aLaptop. Brand & vbTab & aLaptop. Type & vbTab & aLaptop. Price)

Next

End Sub

Compile. If you use the downloaded database, you will find that compilation fails and there is an error:

Error 3023: Problem in Mapping Fragment starting at line 92: Column NotebookStorage. Weight in table NotebookStorage must be mapped: It has no default value and is not nullable.

E: \... \ EFWalkThrough \ NotebookDb. edmx 93 15 EFWalkThrough

It seems that the column you just deleted is not allowed because there is a constraint in the database that requires this column. We need to add it back. Open the Object Data Model Designer, right-click it, add a "Scalar Property" and give it a name: "weight". Set the "Nullable" attribute to "False ".

Then, in the ing details panel, use the drop-down box to map the Weight column in the table to the new "Weight" attribute.

Modify the output statement as follows:

Console. WriteLine (aLaptop. Brand & vbTab & aLaptop. Type & vbTab & aLaptop. Price & vbTab & aLaptop. Weight)

Okay, F5, compile and run. The output result is as follows:

In the output result, all the weights are changed to 2? Well, let's take a look at the data type of weight to see why. Pai_^

 

Summary:

In this article, we create a data entity model Step by step. Then, we modify the ing relationships in the entity model (delete ing and add ing ); we also use LinQ to Entity to query and output data from the object set to the screen.

 

Download the source code in this article.

 

=== Returned index ===

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.