Entity Framework POCO Classes

Entity Framework POCO Classes

Generate POCO Classes Entity Framework

The Entity Framework has come really nice features out of the box when using Entity Framework 5 with Visual studio 2012. Specifically, if you are using a Database First approach, the generated entities from your (.edmx) are already POCOs. No more are the classes auto generated and inheriting from ObjectContext as in past versions.You could achieve this affect either manually or via POCO generation extensions, but it is done by default now in Visual studio 2012.
There are a few things you need to do to separate the POCOs generated under the .tt branch under the .edmx file in Visual studio.  remember that the .tt file is just to support deign time tooling and is not compiled with the project. it brings along with it no dependencies to EF.
Now its time to show how its work!
Step 1:
First of all Create Basic Structure of project as following.
Create project as per shown in above picture.
Create project as per shown in above picture.
Step 2:
Second thing is to add edmx in project,  for that right click on MVC5Test.Repository -> Add New Item and from Data section select ADO.NET Entity Data Model, Name it  And Add. and process further as per below images.

From Data tab select ADO.NET EF and Add
From Data tab select ADO.NET EF and Add

Select EF Designer From Database and select Next.
Select EF Designer From Database and select Next.


Change Data Source and Select Server and Process Further as per Default
Change Data Source and Select Server and Process Further as per Default 
Step 3:
Now, make sure to build the solution to have all the metadata generated in the .csproj file. Navigate to the project containing the .edmx file, and open the .csproj file for the containing project using any editors eg. Notepad. and  remove the XML element that creates a dependency between the .edmx file and the .tt template file.

Remove MVC5Test.edmx dependency
Remove MVC5Test.edmx dependency


now you can drag the .tt into another layer. Remember, the .tt file is a design time tool and adds no dependencies or references to EF, Go ahead and drag the object in solution explorer as shown below.

Drag .tt file to Model and remove MVC5Test.tt file from Repository
Drag .tt file to Model and remove MVC5Test.tt file from Repository
Step 4
There are few more steps to complete the Process. 1st, we must update the path to the .edmx in the .tt file for that open .tt fille and do as per shown below:
Inputfile string variable Edit and Update its Value like showing above
Inputfile string variable Edit and Update its Value like showing above.
now update the namespace for which the .Context.tt file under the .edmx is using. we must provide the namespace of the new location Because we have now moved the POCO classes to another layer. The proper way is to update the Custom Tool Namespace property on the Context.tt properties as displayed below:


Link Model to edmx by providing Custom Tool NameSpace
Link Model to edmx by providing Custom Tool NameSpace
The one step we must remember now is to run the Model.tt file manually now when we want our POCO classes to reflect an updated model. However it is as simple as right-clicking on the .tt file where the POCO classes are located and select Run Custom Tool as shown below.
Use Run Custom Tool to Update Model
Use Run Custom Tool to Update Model

Now the Final step is add reference. Add MVC5Test.Repository and 
MVC5Test.Model reference to Main MVC5Test Project and MVC5Test.Model reference to MVC5Test.Repository project as below.
 
Add Reference to link POCO Classes Structure
Add Reference to link POCO Classes Structure 
Its Done, now you can build your Web development further. ☺☺

Comments