Last time I talked about how to create an ORM using EF's "database first" method. Today I will talk about "code first" method to create an Orm;
Literally, the first priority of a database is to create a database. The first priority of a code is to write code first. Therefore, the first priority of a code is to write code first, that is, it is the opposite of database priority.
First, open vs2010 and create an empty ASP. net mvc 2 project named rororowocodefirst. For example:
The project structure after successful creation, such:
Now we create a code-first ORM model for this project. Right-click the project and choose "add"> "new item". In the displayed dialog box, select "data ", then create an "ADO. net object data model ", such:
It will create a model1.edmx file by default, and then go to the next step, will let you choose whether to generate from the database or from the Toolbox design conceptual model, here we choose to generate from the Toolbox design conceptual model.
After the selection is complete, a blank interface will appear. The Toolbox on the left contains the tools we want to use, such:
From the toolbox, we use the "entity" tool to drag an "entity" to the blank space, such:
By default, the system creates a class named "entity 1" and has an ID attribute field. Double-click over it to modify "entity 1, change "entity 1" to "blogcategory", add the attribute to it, and change it to the following result:
Now, let's check the code. The development tool has helped us generate the ORM code, for example:
The above is the code generated based on our design. Of course, you can also manually write the code without using the design tool. ^ _ ^ is also acceptable. Next, how to create a database? See the following figure:
At this point, we can right-click the interface according to the design, and select "generate Database Based on Model" to obtain the database SQL code generated by the development tool based on our code, used to create a database. Next time, I will talk about the POCO method.