Entity Framework Quick Start-codeonly poco

Source: Internet
Author: User

Previous articlesArticleI briefly introduced the concept of Entity Framework and introduced database first and model first instances. Today we will introduce another exciting development method: codeonly! Of course, EF's support for poco (plain old CLR object) is a major feature in ef4.0. It is also a major improvement in EF scalability. This is also a simple demonstration of the operation steps! Let's briefly talk about the process and then look at the following!

Process: design the entity model in the edmx designer → compile the corresponding entity Class Based on the entityCode→ Write database access interface gateway → Test Result

Let's start our EF poco trip!

Step 1: Create a project

Step 2: design the entity model

On the project, right-click Add new project, add ADO. Net object data model, and select create with empty model. In the entity model designer, right-click the attribute to set the Code Generation Policy of the entity model: None. This step is set to prevent EF from automatically generating entity class code for us, instead, we define the code of the entity class by ourselves, which is more flexible. In addition, we can expand the entity class to make it more convenient and more maintainability. Shows the settings:

Then add the following two entities, as shown in:

In this simple description, there is a one-to-multiple relationship between two entities. This only requires us to add an association in the entity model designer.

After this step is completed, we still use the model to generate the database! [I will not talk about it here. You can refer to the previous article]

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

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

The Department class code is as follows:

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

Namespace Efpoco
{
Public Class Department
{
Public Int Id { Get ; Set ;}
Public String Name { Get ; Set ;}
Public String Master { Get ; Set ;}
PublicIlist<Car>Car {Get;Set ;}
}
}

The car class code is as follows:

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

Namespace Efpoco
{
Public Class Car
{
Public Int Id { Get ; Set ;}
Public String Name { Get ; Set ;}
Public Int Departmentid { Get ; Set ;}
Public String Size { Get ; Set ;}
PublicDepartment {Get;Set ;}//Corresponding foreign key relationship
}
}

Note: the green part is a little different from other code, because there is a one-to-many (CAR) Relationship between two tables, so when designing this entity class, A car can have only one department object attribute, while a department should have one car set object attribute.

In additionEntity classes can be placed in other assemblies respectively.Is not restricted to edmx projects! This feature is also very exciting!

Step 4: Create a database Access Gateway

Add a class code file for a separate database Access Gateway and add the following code:

 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 ;}
}
}
}

In fact, the above code is very simple. It defines a class that inherits objectcontext, and adds the collection of the car and department object objectset in this class.

In this way, all our preparations will be taken. Here is the result of testing your codeonly...

Step 5: test your results


 Using  (Companycontext CC  =     New  Companycontext ())
{
VaR result
= From C In Cc. carset
Select C;

Foreach (VAR m In Cc. carset)
{
Console. writeline (
String . Format ( " Name: {0} | size: {1} \ r \ n " , M. Name, M. size );
}
}


The operation is correct!

Summary:

We didn't use the code automatically generated by EF, but used its Model Designer to help us generate the definition file (XML) of CSDL/MSL/SSDL ). then we write the entity class code and write a Database Access Gateway objectcontext! This brings us a milestone. We can extend the entity class to a domain model, and the code is more flexible and not subject to the Automatic Generation of EF! As we all know, the code automatically generated by EF has a drawback. First, it is not flexible. Then, you can modify the code above and use the model to update the code. Then, the modification will be washed away! This is a headache, but with the support of Poco, we can expand the entity class and sort out existing resources, it also provides the basis for subsequent upgrade and maintenance!

 

Entity Framework Quick Start-index stickers

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.