Build a generic repository with the Entity Framework.
In the previous series of articles about M-V-VM we used the AdventureWorks database, available on CodePlex.
After this article (configure a data layer with LinQ-to-SQL) I have got some readers emails asking me this and that. So, I am going to give you all those answers right now.
Download and configure the Adventure Works database.
On my dev PC I have SQL Server 2008 Standard version, but this tutorial is working also with the express or any other version.
- Go to CodePlex Adventure Works web site project and download the AW available version (light and full). I have downloaded the package called Adventure Works All Databases.
- Go into the folder tree Tools/Samples and you will find the .mdf and .ldf and also the T-SQL script if you want to run it from a prompt command line.
- Open your SSMS and from the master database, install the version that you prefer. My samples work with both version, as I use always the Adventure Works LT (Light version).
- Mandatory: as I use Windows Authentication in all my samples, you must fix that in your local Db and change the connection string in my samples.
Open Visual Studio and create the folder three.
As we are going to work with an enterprise application, in order to simulate the 3 tiers, I have created 3 solution folders, one for the data service, one for the client part and one for the test. We will expand these folders during the building process.
The initial solution three will be:
Solution name: PrismTutorial
Consuming Service: PrismTutorial.WCF
Working with Entity Framework.
In the first series of tutorials, we used LinQ-to-SQL because:
- Its easier, Its faster, It uses the relation 1 to 1 with the database tables and so on.
For this tutorial we are going to use the Entity Framework because this one is the real OR/M coming from Microsoft so in an enterprise application its better to spend more time and effort on building something stronger and reusable.
Open you VS data layer solution, right click and add an new component, ADO.NET Entity Data Model and configure it to reflect the Adventure Works LT database installed into your machine.
After that VS will show up a entity model designer window very similar to the one that we used with Linq-to-SQL.
If you want to be compliant with my settings, this is how I have configured my Entity Framework:
Its time to play, the Entity Framework repository wrap!
Like any software architect, I try to fit always everything in a more generic, reusable and more readable pattern. If we want to have a reusable UnitOfWork associated with a Repository engine, we must wrap the Entity Framework inside something more generics. Here comes the pain .
The final result I would like to have is this one: