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