Ef poco application Series 2 -- getting started with the example

Source: Internet
Author: User

The previous series briefly introduced what is POCO. If you do not know about the concept of POCO, click here to view the content of Series 1. This series starts

POCO usage, Quick Start through simple examples, the database used is the Northwind database

Step 2-create a model and disable Default Code Generation

1. Open VS2010 to create a class library and name the projectNorthwindModel,This project ignores database persistence and dependency on EF.

2. Create a new class library named "NorthwindData", reference "System. date. Entity", and addThe dependency of the NorthwindModel project.

3,Add ADO. NET data model to the NorthwindData project, name the project"Northwind. edmx "for example:

 

4. Create a model for the "Northwind" database through "create from database.

5. selectCategoriesAndTwo Products tables.

6. SelectThe Northwind. edmx attribute clears the content of the custom tool and removes the automatically generated code. For example;

Next, we will compile the POCO entity.

Step 2: Compile the POCO entity code

InAdd to the NorthwindModel ProjectThe code for the Category. cs and Product. cs classes is as follows:

    public class Category    {        public int CategoryID { get; set; }        public string CategoryName { get; set; }        public string Description { get; set; }        public byte[] Picture { get; set; }        public List<Product> Products { get; set; }    }

This class contains the "product" set type attribute. The following defines the product class.

Public class Product
{
Public int ProductID {get; set ;}
Public string ProductName {get; set ;}
Public int SupplierID {get; set ;}
Public string QuantityPerUnit {get; set ;}
Public decimal UnitPrice {get; set ;}
Public Int16 UnitsInStock {get; set ;}
Public Int16 UnitsOnOrder {get; set ;}
Public Int16 ReorderLevel {get; set ;}
Public bool Discontinued {get; set ;}
Public Category {get; set ;}
}

Public class NorthwindContext: ObjectContext
{
Public NorthwindContext (): base ("name = NorthwindEntities", "NorthwindEntities") // database connection
{
_ Categories = CreateObjectSet <Category> ();
_ Products = CreateObjectSet <Product> ();
}

Public ObjectSet <Category> Categories
{
Get
{
Return _ categories;
}
}
Private ObjectSet <Category> _ categories;

Public ObjectSet <Product> Products
{
Get
{
Return _ products;
}
}
Private ObjectSet <Product> _ products;
}

The implementation of these entities is pure POCO entities. You can use these entity classes for persistence operations. The only difference between these entities and the entity classes generated by the EF framework code is that they are POCO entities, you can run unit tests for some simple operation tests.

[TestMethod]
Public void QueryForCategoriesReturnsRows ()
{

Var categoryCount = context. Categories. ToList (). Count;

Assert. IsTrue (categoryCount> 0 );
}

The previous series briefly introduced what is POCO. If you do not know about the concept of POCO, click here to view the content of Series 1. This series starts

POCO usage, Quick Start through simple examples, the database used is the Northwind database

Step 2-create a model and disable Default Code Generation

1. Open VS2010 to create a class library and name the projectNorthwindModel,This project ignores database persistence and dependency on EF.

2. Create a new class library named "NorthwindData", reference "System. date. Entity", and addThe dependency of the NorthwindModel project.

3,Add ADO. NET data model to the NorthwindData project, name the project"Northwind. edmx "for example:

 

4. Create a model for the "Northwind" database through "create from database.

5. selectCategoriesAndTwo Products tables.

6. SelectThe Northwind. edmx attribute clears the content of the custom tool and removes the automatically generated code. For example;

Next, we will compile the POCO entity.

Step 2: Compile the POCO entity code

InAdd to the NorthwindModel ProjectThe code for the Category. cs and Product. cs classes is as follows:

    public class Category    {        public int CategoryID { get; set; }        public string CategoryName { get; set; }        public string Description { get; set; }        public byte[] Picture { get; set; }        public List<Product> Products { get; set; }    }

This class contains the "product" set type attribute. The following defines the product class.

Public class Product
{
Public int ProductID {get; set ;}
Public string ProductName {get; set ;}
Public int SupplierID {get; set ;}
Public string QuantityPerUnit {get; set ;}
Public decimal UnitPrice {get; set ;}
Public Int16 UnitsInStock {get; set ;}
Public Int16 UnitsOnOrder {get; set ;}
Public Int16 ReorderLevel {get; set ;}
Public bool Discontinued {get; set ;}
Public Category {get; set ;}
}

Public class NorthwindContext: ObjectContext
{
Public NorthwindContext (): base ("name = NorthwindEntities", "NorthwindEntities") // database connection
{
_ Categories = CreateObjectSet <Category> ();
_ Products = CreateObjectSet <Product> ();
}

Public ObjectSet <Category> Categories
{
Get
{
Return _ categories;
}
}
Private ObjectSet <Category> _ categories;

Public ObjectSet <Product> Products
{
Get
{
Return _ products;
}
}
Private ObjectSet <Product> _ products;
}

The implementation of these entities is pure POCO entities. You can use these entity classes for persistence operations. The only difference between these entities and the entity classes generated by the EF framework code is that they are POCO entities, you can run unit tests for some simple operation tests.

[TestMethod]
Public void QueryForCategoriesReturnsRows ()
{

Var categoryCount = context. Categories. ToList (). Count;

Assert. IsTrue (categoryCount> 0 );
}

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.