In Entity Frame Work (hereinafter referred to as EF), two methods are supported: Database priority and code priority;
EF is An ORM framework launched by Microsoft. Before that, Java had a very mature ORM framework, such as hibernate. in the. NET version, the name is nhib.pdf, n more). I will only talk about EF this time, And nhib.pdf will talk about it again next time;
Let's talk about database first. Database first refers to creating a database first, including creating tables and fields, and then generating the ORM code based on the database. It refers to creating a database first, create related program code;
Next, let's take a look at the specific operations.
First, create a database and tables in a database, such:
Create an "ASP. net mvc 2 empty web application" with the project name "rorowodatafirst", for example:
After a project is created, vs2010 automatically creates a directory structure and basic file resources based on MVC 2, such:
Now we create a database-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 conceptual design model, here we choose database generation, (note: the conceptual design model is designed using the toolbox in vs2010, which is the code-first method we will talk about later). Click Next and we will choose database connection, if you have not created a database connection, there will be a new connection next to it. click the button to create a new connection. Here, my connection information is as follows:
Select to include sensitive data in the connection string.
Select the tables and views to generate.
The object model graph after the creation is successful.
After the controller is created, how can we use it? Create a default controller and the code is as follows:
Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Using rorowodatafirst. models;
Namespace rorowodatafirst. Controllers
{
Public class defaultcontroller: Controller
{
//
// Get:/default/
Public actionresult index ()
{
// Use the context object of the orm object framework
Rorowodbentities context = new rorowodbentities ();
Blogcategory model = new blogcategory ();
Model. catename = "first category ";
Model. createtime = datetime. now;
// Add a category to the database
Context. addtoblogcategory (model );
Context. savechanges ();
Return view ();
}
}
}
Is it very convenient and powerful? If you have used hibernate, do you think it is very easy to get started?
Next time, I will talk about how to create an ORM in the code-first mode.