Entity Framework 4 in Action Reading Notes-Chapter 2: getting started with Entity Framework (2)

Source: Internet
Author: User
Tags import database

In this article, we will build our applications.

1. Create an assembly

1. In VS2010, create an empty solution named OrderIT.

2. Add a class library project named OrderIT. Model in the solution, and add the reference System. Data. Entity.

3. Add an ASP. net mvc 3 Application Project in the solution and add the OrderIT. Model application.

After completing the above steps, the final effect is as follows:

2. Use the database-first method to design entities

Prepare the database used by the program before you start.

Database script download

Create a new database named OrderIT in the database, and then execute the above script.

Import Database

1. Add a new item to OrderIT. Model, select ADO. NET Entity Data Model, name it Model. edmx, and click Next.

2. Select Generate from Database and click Next.

3. Select a connection or create a connection.

4. Select the table shown in the following figure and click Finish.

Detailed steps are shown in:

The final effect is as follows:

Create an object from scratch

1. to delete a Company object, a dialog box is displayed. Select "NO". Even though the object is deleted, the database information is saved in the EDMX file.

2. Right-click the designer and choose Add> Entity.

3. In the Add Entity dialog box that appears, Set "Entity Set" to "Company", "Property name" to "CompanyId", and click "OK", as shown in.

4. The Company entity appears in the designer. Right-click the Company entity, select Properties, and set Abstract to True in the Properties window.

5. Right-click the Company object, select Add> Scalar Property, type Name, and press Enter. In this way, the Name attribute is added to the Company class.

6. Right-click the Name attribute and select Properties. In the Properties window, Set Max Length to 30 (the value is consistent with the Length of the ing column in the database ).

7. Also add the Version attribute. In the Property Window, set Type to Binary and StoreGeneratedPattern to Computed.

In this way, the Company entity is created.

Create AddressInfo before creating a Customer object.

1. Right-click the blank area of the designer and choose Add> Complex Type.

2. Open the Model Browser window, type AddressInfo, and press Enter.

3. Right-click AddressInfo, select Add> Scalar Property> String, type Address, and press Enter.

4. Right-click the Address attribute and select Properties to change the Max Length to 20.

5. Repeat 3-4 steps to add the City, Country, and ZipCode attributes. As shown in:

Create the Customer entity and Supplier entity

1. Right-click the blank area of the designer and choose Add> Entity.

2. In Add Entity, set Entity name to Customer, Base type to the Company you just created, and click "OK ".

3. Right-click the object, select Add> Complex Property, type BillingAddress, and press Enter.

4. Right-click BillingAddress and select Properties. In the Properties window, set Type to AddressInfo.

5. Based on the analysis in the previous article, add the Complex Property, Username, Password, and WSEnabled Scalar Property named ShippingAddress to the Customer. Do not forget to set the Max Length and the WSEnabledType attribute to Boolean.

6. Create a Supplier object, add the Scalar Property named PaymentDays and IBAN, set the Type Property of PaymentDays to Int16, set the Fixed Length of IBAN to True, and Max Length to 27.

Create a ing

1. Right-click the Company object and select Table Mapping.

2. In the detaing Details window, select the Company table. When the owner name matches the column name, The ing is completed automatically. If the owner name does not match the column name, The ing must be completed manually.

3. Right-click the Customer object and select Table Mapping.

4. In the detaing Details window, select the Company table. Select the attribute that matches the table column in the right column. For example:

5. Click Add a Condition and select Type. Enter C in Value/Property, as shown in.

6. The Supplier operation is the same as the Customer operation, for example:

Modify entity

The Order class does not need to be changed. You only need to delete the address-related attributes and add an AddressInfo type, that is, delete ShippingAddress, ShippingCity, ShippingCountry, and ShippingZipCode to add a ShippingAddress. Then modify the ing.

Modify the inheritance relationship between Product, Shoe, and Shirt.

1. Right-click the association between Product and Shoe and select Delete.

2. Right-click Product and choose Add> Inheritance.

3. In the designer dialog box, select Product as base entity and Shoe as derived entity.

4. Delete the ProductId attribute in the Shoe object.

5. Select the Shoe object to open the Mapping Details dialog box and map the ProductId column to the ProductId inheritance attribute.

6. perform the same steps on the Shirt.

Design relationship

1. Right-click the Customer object and choose Add> Association.

2. In the Add Association dialog box that appears, select as shown in the figure.

3. Click OK and double-click the association line between the two entities. In the reference constraints dialog box that appears, select Customer from the Principal drop-down list, which represents the primary entity in the relationship. In the following table, select the CustomerId attribute of the Order object as the foreign key. This indicates that the other side of the Link (Order) is associated with the primary key through the CustomerId attribute. For example:

4. Right-click the link above, select Properties, and set the End1 on Delete attribute to Cascade.

The above steps are only applicable to one-to-many relationships. For many-to-many relationships, use the following method.

1. Create an association between the Product and the Supplier entity.

2. Right-click the association line between two entities and select Table Mapping.

3. In the detaing Details window, map to the ProductSupplier table.

4. Automatically map the ProductId and select SupplierId for Supplier, for example:

The designed model is as follows:

Words written behind

Now the model has been designed, but there are still questions to answer. Where is the design code? How can I customize the generated code? How to access the database? The next article solves these problems.

 

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.