Friday, June 4, 2010

ADO.NET Entity Framework 4.0 - Does it work??? – Yes it does and below is an example in 6 easy steps.

The first question I had with the entity framework is if the framework actually worked.  As many of us already use tools such as SubSonic, NHibernate, etc, we often question if the ADO.NET Entity Framework 4.0 will provide the same coding benefits as the other tools.  To make the blog entry brief, the answer is yes.  In fact, Microsoft .NET’s ADO.NET Entity Framework 4.0 exceeded my expectation in several areas where the other products either required some additional configuration or code to handle the same simple tasks.

6 Steps to using the Entity Framework based on an already existing database design

Step 1:  

Create a Database (EntityTestDB).  Then add a few tables such as Users, Roles and Profiles.  Link the tables accordingly so that you build a relationship between the User, Role and Profile tables.



 










Step 2:  

Create a New Web Project using Visual Studio 2010.  (For this example, select the MVC 2 Template project).  Once the project is created, Add a new folder called Business Layer with a Business Layer class in that folder.



















Step 3: 

After creating the base project and Web Solution you are now ready to use the ADO.NET Entity Framework.  To do so, right click on the solution and click “Add New Item”.  Click on the “Data Template” Item on the left and then select “ADO.NET Entity Data Model”.  For this Step Name your Entity “EntityTest” and click “Add”.


At this point a wizard will be shown with a number of automated steps to assist you with creating the necessary items for your project.
  • First will be to choose the model contents.  For our example we are going to use the database we created in the first step so select “Generate from Database” and click “Next”.

    • Next either create a new connection or connect to the location where you created your database in Step 1. Then proceed to the next step in the Wizard. (You can leave the default checkboxes for this step.)

    • Next you are going to choose what objects you would like for your Model.  For our example select them all and click “Finish”.  “Tables, Views, and Stored Procedures”.  (You can leave the default checkboxes for this step.)


    • When the Wizard finishes you will be presented with a Model View of your tables. 

    At this point there are several items to be aware of that are very valuable.  First is the Table Mapping Details. (Just “Right Click” on the Model Object and Select “Table Mapping”)













    The second is the Mapping Details for your Operations such as Insert, Update, and Delete.  (Just click on the other icon on the left in the Window showing the Mapping Details.












    Step 4:

    Create a Select Function and map this to the stored procedure we created in Step 1 to retrieve the User, Profile and Role data from the database.  A quick way to perform this step is to use the Entity Model Browser.  To view the Entity Model Browser “Click on View” from the menu bar, then Other Windows, then “Entity Model Browser”.  



    Note: 
    The entity browser will only show you your entity details if you have selected the entity model from Solution Explorer.

    Next you will Navigate in the Entity Model Browser to the “Stored Procedures” section.  Then expand that window and “Right Click” on the stored procedure to select the User, Role and Profile information.  Then click on “Add Function Import”.












    For our example we would like to return a list of Users so when the Wizard starts, make sure to select the User entity as the return type and click “OK”.





























    Step 5:

    Now we are ready to use our Entities, Models and call our Stored procedure.  Navigate to the Business Layer and create a method named “TestEntityFramework” in the class.

    Note:
    For this example it does not matter what is returns or anything we are just showing the functionality.  If you like you can add a grid to the screen and bind the return data to the screen.

    Add the following code to the sample:

















    Step 6:

    From your sample make a call to the “TestEntityFramework” method and view the results.

    SUMMARY

    In summary, Microsoft’s Entity Framework 4.0, the Entity Model Browser and the overall functionality seems to be on par if not better than the other tools that have been used in the past.  The advantages that I noticed were the ability to easily configure the entity framework for LINQ to SQL, Stored Procedures, etc.  In addition, the relational database mapping of the objects is awesome.  There are many more reasons to use this over other tools, and this was more of a tutorial on how to use the ADO.NET Entity Framework 4.0 in Visual Studio 2010 then an evaluation of one tool-set over another.

    Enjoy!!!!

    1 comment:

    Kon said...

    Pretty cool. SubSonic, your move!