Friday, August 8, 2014

Entity Framework

If all of your databases already exist and you are acting upon existing data. Entity Framework 6(EF) allows you to to build an Entity Data Model XML(edmx) file based on an existing database. You can see an excellent tutorial here. EF is an Object to relational Mapping (ORM ). What EF gives you is actual objects or DbSet's to act upon with linq.

As you can see it built our Database structure and gave us DbSet's that match our tables, and methods to match our Stored Procedures. Now lets look at the individual table objects.

C# is cleaner because of Auto Properties. But on either account you can now access your data layer using linq and get data objects that will directly map back to the database. Now we can wrap this functionality in a repository so that our data layer can be separated from your service code.(Single Responsibility Principle)
Now we have covered all of our CRUD actions. Using EF allows you to act on the data layer without using Stored procedures for all of your CRUD actions.

Now you have seen how EF gives you access to existing objects, but what if we want to extend one of those objects or even add an Interface to one of them so that we can use Dependency Injection? Well EF uses partial classes to construct all of the entities and models. So we can extend them or wrap them in interfaces by making our own object of the same name.


In conclusion: EF is a wrapper for your Database and with the ability to create our objects from existing tables we can quickly write code to do any action needed against the database. We also get our existing stored procedures so you don't have to "reinvent the wheel".

Robert T. Frey

No comments:

Post a Comment