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

Source: Internet
Author: User

In this chapter, we will look at a classic Order application.Program"Orderit", which manages product, order, and customer information.

Requirement Analysis

1. Store customer information. Each customer has a name, Bill address, and delivery address. The address is not a plain text, but composed of streets, cities, postal codes, and countries. More importantly, customers can access the Web service to add and update orders. Therefore, they need a user name and password to access the Web service.

2. Store supplier information. Each supplier has a name, an international bank account number (IBAN), and a customer's payment terms (the number of days the customer pays the invoice ).

3. Store product information. The system must be able to sell any products, from shoes to shirts, from computers to tennis rackets. In addition, applications can easily add new products without affecting previous products and related orders.

4. Track the current inventory. The inventory of products to be sold and purchased must be updated. You can add new products to the current quantity.

5. Any products sold by storage vendors. One supplier can sell many products, and one product can also be purchased from many suppliers.

6. Manage customer orders. Each order must be stored under which order is placed, and the customer can choose the shipping address stored in the database. A customer can buy many products at a time, so an order contains multiple details.

7. Calculate the applicable discount. The discount policy stipulates that, if a customer buys more than five identical products, each product will be entitled to a 10% discount. If discounts are applicable, the order must contain two details: the first is the information of the first five products without discounts, and the second is the information of the products with other discounts and related discounts.

8. Users can easily and quickly modify product prices. Sometimes users need to modify the price of a product, and sometimes they need to update the price (such as the product type or brand) based on features ).

9. allow customers to view and modify account details and orders. Orderit exposes a web service. You can use it to add, update, and delete orders, and modify your account information.

The above requirement analysis is shown in:

Design orderit models and databases

Before starting, let's talk about"Bottom-up (bottom-up)AndTop-down).

Before designing an application, you must determine what to create first: database or object model.

Designing a database first is a bottom-up method. It is most commonly used for two reasons: first, data-centric applications. Second, the object-oriented design in Microsoft stack is a new technology, so the design model is not too common before the database design. In other cases, database administrators have a lot of authority, especially in large companies that store large amounts of data. Database Administrators often consider databases first. The "bottom-up" design ensures the best performance and storage optimization. If the database administrator works well, you may only need to use the stored procedure and database interaction to hide the underlying database structure. On the other hand, this method means that data is organized according to the needs of the database, and data separation is difficult for applications to use.

Designing an object model first is a top-down approach. The main advantage of top-down is that applications are almost independent from databases to shape their models. The disadvantage is that sometimes it may not be conducive to the organization of the database, and may also lose performance.

Databases are an important part of applications. Therefore, model design and database design should be seamlessly combined to better use them. The top-down design focuses on the model and business, rather than the data organization, but if it affects the performance of the database, we should discard it and use the bottom-up method. We believe that "top-down" design is the best method. In actual projects, we can find great benefits from this method, and the database performance is also good. Although the "bottom-up" method is still common, the "top-down" design method is increasingly popular.

The Entity Framework designer can use the above two methods. You can visually design model classes, and then let the designer generate the ing information and database scripts (top-down, model-first). You can also import the database and let the designer generate classes, and one-to-one boting with database tables (bottom-up, database-first ).

Customers and suppliers

The customer has the bill address and delivery address, and the order has the delivery address, so create a complex type of addressinfo first. Create an abstract class named company to inherit the customer and supplier classes from company. Create a company table when designing the database, put all fields required by customer and supplier into the company table, and add a type column to specify whether a row belongs to customer or supplier. The primary key is companyid, which increases automatically. For example, the company table is on the left and its inheritance hierarchy is on the right.

Product

Create a product base class and create a class for each product so that they all inherit from the product class. Create a product table when designing the database, including the public information of all products, and then create a table for each product. For example, the shirt table and shoe table. The primary keys of all tables are productid, And the ID column is used in the product table, but not in other tables. For the correct association record, the primary key in the product table must match the primary key in the child table. Shows the structure of the table and model class:

According to the requirements of orderit, you must track the sales status of the products provided by each supplier, because one product can be provided by multiple suppliers and one supplier can provide multiple products, therefore, there is a many-to-many relationship between them. In the relational model, add a suppliers attribute of the icollection <supplier> type to the product class, and add an icollection <product> Products attribute of the supplier class. It may be a little difficult in the database, so you cannot add Foreign keys to the child table, because there is no child table at all. The only way is to add a third table named productsupplier, which contains the ID of product and supplier, which together form the primary key of productsupplier. For example:

Order (orders) Class

In the model, create an order and orderdetail class. The first class contains order information, and the second class contains its details. Because you can select the shipping address stored in the Customer Account, add a shippingaddress attribute in order. About the relationship, add a navigation attribute orderdetails in the order class and add a navigation attribute order in orderdetail. An order must have one customer, so the order class must have a customer navigation attribute. Each order details contains a product. Therefore, add a product navigation attribute to the orderdetail class.

In the database, create an order table for all orders and create another orderdetail table for all details. The primary key is the ID column, and the order table contains the address information. When designing the relationship between tables, you must add the following foreign keys:

(1). Order table customerid -- Link order and customer.

(2) orderid of the orderdetail table -- link the order and its details.

(3). orderdetail table productid -- Link order details and products.

Words written below

The requirement analysis and model and database have been designed, and the next article will perform actual operations.

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.