ASP. NET 4.0 and Entity Framework 4-Article 1-create a database using model-first Development

Source: Internet
Author: User

This article is a series of ASP. NET 4.0 and entity framework 4ArticleThe first article, this series will show you how to use entity framewok4 to Develop ASP. NETProgram. The article will teach you how to use entity framewok4 to create a database step by step. The created database will be used in later articles.

Summary

The released Visual Studio 2010 contains entity framewok4, which is the second version of entity framewok (hereinafter referred to as ef) released by Microsoft ). Based on ef 1, according to the suggestions of developers, it has made many improvements, including functions similar to O-R mapping tools such as nhibing. The O-R mapping tool gives us the hassle of retrieving and persisting dataCode. We only need to use visual tools to create mappings between classes and tables, tables, stored procedures, and other database objects. One of the biggest new features of EP4 is: allow us to create an ADO. Net object model and then generate a database based on this model. In the past, the general practice was to create a database and then generate an entity model. Of course, EF4 still supports general database design, but Visual Studio can be used to design databases based on object models, which has been greatly improved.

This document describes how to create an ADO. Net object data model through EF4. The tool used is Visual Studio 2010 beta 2, which may be different from the final release. Later articles will explain how to use EP4 to query and display data, call the stored procedure, and customize the classes generated by EF4.

Create a database using model-first

Step 1: create a solution

1. Run vistual maxcompute studio 2010 beta2.

2. Click "new project" on the start page ".

3. Click the C # Node to view the installed project template.

4. select ASP. NET web application from the template list.

5. Enter the project name "ordersystem" and click "OK ".

In this case, Vs will create a solution in the directory you specified and create a website project under the sub-file of the directory.

Step 2: Create An ADO. Net Object Model

The following describes how to create an ADO. Net object model. For this program, we have two conceptual models: Users and addresses. A user may have multiple addresses, which must be reflected in the model.

1. Right-click ordersystem, select add, and click New item... (or press Ctrl + Shift + ).

2. Click the data subnode under Node C.

3. Select the ADO. NET Entity Data model template.

4. Change the name to "orderdb. edmx" and click "add.

5. Vs will display Entity Data Model Wizard. You can select the model source.

6. Select "Empty model" and click "finish.

Orderdb. edmx will be added to your project, and the Entity Framework designer will be displayed.

 

Step 3: Create an object relationship

Next, we will create the relationship between the user object and the addresses object. First, we will create the user object.

1. Right-click the Entity Framework designer and choose add-> entity.

2. You will see the "add entity" dialog box, and enter the entity name. Note that when you enter an object name, the system automatically generates the name of the object set (the object name is the name of a class that represents a record, after an object set, it is the name of a class that represents a set of records ).

3. By default, vs generates a primary key named ID. If you do not want to generate a primary key, do not select "create key property ". In this example, the primary key is required, so the selection status remains unchanged.

4. Click "OK" and the useraccout entity will be added to the entity chart.

5. Add attributes to the object. These attributes are mapped to fields in the data table. We will add the firstname and lastname attributes.

6. Right-click useraccount. Select Add-> scalar property.

7. Change the attribute name to firstname.

8. In the attribute window, set the maximum length to 50. Do not change the data type. The default value is string.

9. Add another property named lastname in the same way, and set the maximum length to 50.

The following describes the attributes of the record creation time and update time. I personally prefer to add the creation time and update time attributes to each object so that they can be easily reviewed later. These two attributes are added to each object as a complex type. Complex types allow you to define a set of attributes to meet the needs of various entities.

10. In the model browsing window, right-click the complex types node. Sometimes you may need to click a few more times to select the instance. This may be a bug in vs beta2. Click "coomplex types" and select "create complex type" in the pop-up menu ".

11. Change the name of the complex type to auditfields.

12. Right-click the auditfileds complex attributes in the model browsing window. In the pop-up menu, choose add-> scalar property-> datetime.

13. Change the name to "insertdate ".

14. Add the updatedate attribute to the complex type in the same way.

15. Now you can add the complex type to the useraccount entity. Right-click the useraccount entity and choose add-> complex property from the pop-up menu.

16. Change the name to "auditfileds". The default data type is audtfields.

17. Right-click the designer and select Add-> entity from the pop-up menu.

18. Change the object name to "Address" and click "OK ".

19. Add address1, address2, city, state, and zip attributes to the address object. All attributes are of the string type. The data lengths are 50, 50, 50, 2, and 5, respectively.

20. In the Properties window, set nullable of address2 to true. Other attributes are required.

21. Similarly, add the auditfileds attribute to the address object.

The following describes the relationship between the useraccount and the address entity.

22. Right-click the useraccount entity and select Add-> association from the pop-up menu.

23. you will see that the relationship between useracccount and address is set one-to-many by default in the Add Association window. If you want to set different types of relationships, select them in the mutiplicity drop-down box, in this example, retain the default value. Click "OK" and you will see an additional useraccountid attribute in the address table.

 

Step 4: generate a database

The object has been created. The following figure shows how to create a database. The vs operation is very convenient. It will generate a DLL statement script used to create tables, indexes, and table relationships.

1. Right-click the designer and select generate database from model. The generate Database Wizard window appears.

2. Click "New Connection.

3. Enter the server name and select Windows authentication or SQL Server Authentication, depending on the connection method of your database system.

4. Enter "ordersystem" in the Database Name and click "OK ".

5. At this time, Vs will prompt you that the database does not exist. Ask if you want to create a new database and click "yes ".

6. After the Database "ordersystem" is created, it will return to the generate Database Wizard window and click "Next ".

7. The DLL statement used to create a database is generated.

8. Click "finish.

9. You will see an additional orderdb. edmx. SQL file in the project, which contains the DLL statement used to create the database. The file content is as follows:

Set quoted_identifier off; Set ansi_nulls on; gouse [ordersystem] goif schema_id (N 'dbo') is null execute (N 'create schema [DBO] ') go -- export dropping existing FK constraints -- ------------------------------------------------ if object_id (n' [DBO]. [fk_useraccountaddress] ', 'F') is not null alter table [DBO]. [addresses] Drop constraint [fk_useraccountaddress] Go -- export dropping existing tables -- -------------------------------------------- if object_id (n' [DBO]. [useraccounts] ', 'U') is not null drop table [DBO]. [useraccounts]; goif object_id (n' [DBO]. [addresses] ', 'U') is not null drop table [DBO]. [addresses]; go -- -------------------------------------------------- creating all tables -- Creating creating table 'useraccounts' create table [DBO]. [useraccounts] ([ID] int not null, [firstname] nvarchar (50) not null, [lastname] nvarchar (50) not null, [auditfields_insertdate] datetime not null, [auditfields_updatedate] time not null); go -- creating table 'address' create table [DBO]. [addresses] ([ID] int not null, [address1] nvarchar (50) not null, [address2] nvarchar (50) null, [City] nvarchar (50) not null, [State] nvarchar (2) not null, [Zip] nvarchar (5) not null, [auditfields_insertdate] datetime not null, [auditfields_updatedate] time not null, [useraccountid] int not null); go -- Creating creating all primary key constraints -- Creating creating primary key on [ID] In table 'useraccounts' alter table [DBO]. [useraccounts] With nocheck add constraint [pk_useraccounts] primary key clustered ([ID] ASC) on [primary] Go -- creating primary key on [ID] In table 'address' alter table [DBO]. [addresses] With nocheck add constraint [pk_addresses] primary key clustered ([ID] ASC) on [primary] Go -- Creating creating all foreign key constraints -- Creating creating foreign key on [useraccountid] In table 'address' alter table [DBO]. [addresses] With nocheck add constraint [fk_useraccountaddress] foreign key ([useraccountid]) References [DBO]. [useraccounts] ([ID]) on Delete no action on update no actiongo

Note that no tables have been created for the database. To create these tables, open the orderdb. edmx. SQL file and right-click Execute SQL. At this time, Vs will prompt you to connect to the database system, enter the relevant information, and click "Connect". After successful login, these SQL statements will be executed, and the database table will be created after execution.

Summary

The above are all the steps for creating a database using model first of Entity Framework. Compared with previous versions, it has made great improvements. Now you only need to design an object model. Vs will automatically generate statements for creating data tables, indexes, and table relationships.

This is the first article in this series. Later articles will introduce you to entity framework4 and ASP. NET 4.0. The next article will show you how to present the data and use entity framework4 for data addition, deletion, modification, and query.

Loydsheng

Project files: http://files.cnblogs.com/lloydsheng/OrderSystem.zip

Independent blog article link: http://lloydsheng.com/2010/04/aspnet-40-and-entity-framework4-part1-create-database-using-modelfirst-development.html

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.