[Original] tried the Entity Framework poco Function + code first

Source: Internet
Author: User

Added support for poco (plain old CLR object) in Entity Framework 2.0, which makes EF more and more powerful.

The following is an example of self-exploration:

Step 1: Create a winfrom Project (only for testing)

Step 2: design the entity Model and Its Association

Right-click the project and choose "Add new project"> "Select data"> "ADO. NET Entity model ".

:

 

Select create empty model, as shown in:

 

Design the model, add attributes, and add table associations.

In the entity design window, right-click → properties →CodeGeneration Policy => noneIn this step, the entity class generated by EF Automatic Code and the objectcontext of the context gateway for EF data access are removed. As shown in:

Then, add two entities, namely departmetn, car, and complex type wheel, to the Model Designer. And add the object Association, and the department and car are one-to-multiple relationships. Of course, I added a complex type of the wheel type to the car.

As shown in the following figure:

Step 3: generate a database based on the model(Codefirst)

In the model designer, right-click → generate Database Based on Model → select database connection → open the generated SQL code file on vs2010 by default, and right-click to execute SQL

Step 4: Compile the corresponding entity Class Based on the designed entity

Write Department class, wheel class, and car class respectively. The attribute name must be the same as that of the model.

The Department class code is as follows:

As a complex type in the car class, you should first define the car and then define the car. The Code is as follows:

The car class code is as follows:

Step 5: define your own database access object context objectcontext[It encapsulates the network management for accessing the database. All additions, deletions, queries, changes, and operations are performed on the database through this interface]

To add a custom class to inherit the objectcontext, add the objectset set corresponding to the object, and initialize the connection in the default constructor, you only need to call the constructor of the parent class, and initialize the object set in the constructor [callCreateobjectset <T> () method ].

The Code is as follows:

  Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Data. objects;


Namespace Efpoco
{
Public Class Companycontext: objectcontext
{
// Connection string generated by EF
Private Static String Constr = System. configuration. configurationmanager. connectionstrings [ " Companycontainer " ]. Connectionstring;
Public Companycontext ()
: Base (Constr, " Companycontainer " ) // The first parameter is the value of the link string of EF, and the second parameter is the name of the object container. You can find it in the right-click attribute on the object designer model. The key of the default connection string is also the name of the container.
{
Departmentset = Createobjectset < Department > ();
Carset = Createobjectset < Car > ();
}
Private Objectset < Department > Departmentset; // Define the collection of objectsets corresponding to the Department table
Public Objectset < Department > Departmentset
{
Get { Return Departmentset ;}
Set {Departmentset = Value ;}
}
Private Objectset < Car > Carset; // Define the collection of objectsets corresponding to the car table
Public Objectset < Car > Carset
{
Get { Return Carset ;}
Set {Carset = Value ;}
}
}
}

Step 6: Use the designed object context to query table data

On the form, add a button and a RichTextBox A dview. Write the following code:

    Using (Companycontext CC  =     New  Companycontext ())
{
VaR result = From C In Cc. carset
Select C;
This . Datagridview1.datasource = Result. asenumerable < Car > ();
Foreach (VAR m In Cc. carset)
{
This . Richtextbox1.text + = String . Format ( " Name: {0} | size: {1} \ r \ n " , M. Wheel. Name, M. Wheel. size );
}
}

Finally, you will seeProgram. What are you waiting for? Try EF's poco function!

Attachment:Source codeDownload

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.