The functionality added in Entity Framework 2.0 to support Poco (Plain old CLR Object) makes EF more powerful. Let me share an example of my own learning:
First, create a new project
Create a new Console application Efpocotest (the process is no longer described).
Ii. Adding an Entity Data Model
1. At the root of the console application, right-click Add New Item, select Data---Entity Data model, change name to COMPANY.EDMX, click Add. Select the empty model in the Data Model Wizard and click Finish. Then you open a data Model designer in VS, right-click in the blank to select Properties, modify the code generation policy to None (True when EF automatically generates the entity class for us and the code for EF data Access Context Gateway ObjectContext ).
2. Add an entity class, an association between entity classes, and complex attributes. The final effect is shown below. Right-click in the blank of the designer, select Update Database according to the entity model, click Done, by default it is open in VS, click the Execute button in the upper right corner, execute the script, and then generate two tables in the database.
Third, according to the design of the entity to write the corresponding entity classWrite department class, wheel class, car class respectively. The name of the property is the same as the property on the model.
(1) Car class
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;} Public Department Department {get; set;} Public Wheel Wheel {get; set;}} }
(2) Department class
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;} Public ilist<car> Car {get; set;}} }
(3) Wheel class
Using system;using system.collections.generic;using system.linq;using system.text;namespace EFPoco{Public class Wheel {public int ID {get; set;} public string Name {get; set;} public string Size {get; set;} public string Remark {get; set;}} }
(4) companycontext class
companycontext inherits the base class ObjectContext. It is a network management that encapsulates the Access database, and all the additions and deletions are performed on the database through this interface
Using system;using system.collections.generic;using system.linq;using system.text;using System.Data.Objects; Namespace efpoco{public class Companycontext:objectcontext {//EF generated connection string//private static string C Onstr = system.configuration.configurationmanager.connectionstrings["Companycontainer"]. ConnectionString; Public Companycontext (): Base ("Name=companycontainer", "Companycontainer") {department = Createobjectse T<department> (); Car = createobjectset<car> (); The private objectset<department> department;//defines the collection of ObjectSet that corresponds to the Department table public objectset<departme Nt> Department {get {return Department;} set {department = value;} The private objectset<car> car;//defines the collection of ObjectSet corresponding to the car table public objectset<car> car { get {return car;} set {car = value;} } }}
Four, form design background code
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;namespace efpoco{public partial class form1:form< c1/>{public Form1 () { InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) { using (companycontext cc = new Companycontext ()) { C10/>var result = from C in CC. Car select C; This.dataGridView1.DataSource = result. Asenumerable<car> (); foreach (var m in CC.) Car) { This.richTextBox1.Text + = string. Format ("name:{0}| Size:{1}\r\n ", M.wheel.name, M.wheel.size);}}}}
V. Testing
Test success
V. Summary
EF's three design patterns: Dbfirst, Modelfirst, Codefirst. For the difference between the first two design patterns, please refer to my other blog post: Modelfirst, the difference between thetwo design models of Dbfirst and the two types of updates . The latter two design patterns are similar (code generation policy design is different), are based on the entity model to generate the corresponding database. Beginner EF, in this example design encountered the following problems, I hope you can warning, to avoid re-emergence
(1) Code generation policy forget to modify, do not modify is Modelfirst, modify to No is Codefirst
(2) The attributes of the entity class are inconsistent with the database (because of the need for hand knocks, careless words will make mistakes)
(3) Connection string configuration error in the context of the entity model (the concept of entity container is not clear)
(4) Wheel is a complex data type, you should first create the wheel class, and then create the car class
Try to combine the Entity Framework poco functionality with the Codefirst