A Quick Guide to Using Entity Framework (ADO.NET)
If you’ve spent some years developing applications you are almost absolutely certain to be familiar with SQL. It is powerful but it is certainly not convenient to have to learn a completely different syntax and programming language to work with your data.
Then there’s the problem of database design. Most programmers work with Object oriented design and find it really hard to translate these objects into the Set based structure of an RDBMS. Normalization is a pain!
That’s why most projects need a database specialist. Someone who can make sense of all the data and make sure the business logic connects well to the database.
Entity Framework Translates Relational Data into Objects
Taken in its simplest form, Microsoft’s Entity Framework is a O/R Mapper, i.e. an Object/Relational Mapper. It expresses the relational data in the database in form of Object that a programmer can use easily in his or her projects without worrying about relational queries and tables.
That solves a major problem, because programmers can directly implement their object oriented business logic and plan their applications better.
Entity Framework is slightly more than an O/R Mapper
In case you have been following programming news, you’re probably aware of other O/R Mappers. There’s Nhibernate for instance. Entity Framework is a more special because it integrates so well with Microsoft’s .Net paradigm and Visual Studio.
It also gives you access to Linq To Entities. LINQ is a great programming innovation that makes database queries a first class member of your program. You can integrate it right inside your business logic, no need to write special functions to retrieve your data.
So now that you’re convinced how cool Entity Framework is, let’s learn how you can use Entity framework in your projects and access database in a way you’ve never done before.
Get Entity Framework
Entity Framework is free. You can download it from Microsoft’s website. The latest version is Beta 3. You’ll also need the Visual Studio integration kit, .Net Framework 3.5, Visual Studio 2008 patch and Visual Studio SP1 upgrade. Quite a list; isn’t it? But definitely worth downloading.
Installing Entity framework is a little tricky. But here’s a link to a great guide that should make the job easy for you: http://www.installationwiki.org/Installing_ADO.NET_Entity_Framework
Let’s Make our First Entity Framework App
Entity Framework works as a layer that sits between your code and your database. Microsoft has provided a nice wizard to help you integrate Entity framework.
Add a new Entity Framework Model
Right click on your project in the Solutions Explorer, click on ‘Add new Item’ and choose ‘ADO .Net Entity Data Model’ from the dialog that appears. This item will only appear if you have successfully installed Entity Framework on your computer. If it doesn’t appear, check your installation.
This will bring up the Entity Data Model Wizard.
You can choose to ‘Generate from database’ or start with an ‘Empty Model’. In almost all the cases you would be Generating from a database because you don’t want to create an empty model and implement your logic by hand.
The next window you see will ask you to choose a connection for the database. If you’ve ever used the Data Model Wizard to create a connection earlier, you’ll see it in the list. Otherwise you will have to create a fresh connection by clicking on the ‘New Connection’ button which will bring you the following dialog box.
Here you can specify your data source, your server name, and the database login ID and the password if needed. Based on your input the Data Model Wizard will create a connection string. You can test your connection using the ‘Test Connection’ button to ensure that the connection is working alright.
When you click on ‘Next’ again the EDM Wizard will load the database objects and ask you to select the objects you wish to include in the Model. The
The new version of the Entity Framework supports Stored procedures as well. Check the tables, views and stored procedures you want to include in your model and press Finish. This will generate the classes you need to work wit the database, and you will also see a graphical view of your Entities and the relationships between them (using primary and foreign keys).
Our sample database has no relationships. Each block represents one object in the Entity model. You can work with. Now your objects are created. Entity Framework gives you a great deal of freedom in deciding how your objects are going to look and how they are going to behave.
In fact you can change the names of the Object properties from what they are in the database and also assign Stored procedures to common functions like Insert, Delete and Update. All this id done by the Object Mapper displayed at the bottom of the Entity Diagram.
Using the Mapper you can give your properties more descriptive names, and if you assign stored procedures to the functions, then the Entity framework will make it very easy for you to Save and Update your data.
Whew! Setting up the entity framework in your project is a time consuming job, but all that effort is going to pay you back many times in code. You are going to have to write less code to deal with your data. Let us learn how.
Working with Entity Framework in Code
In the Solutions Explorer, expand your Entity Model and you’ll see a Designer file for it. Double click the Designer file to open it. You will see the Class name with which you can access the Entity model. This should be the same as set by you when you created the Entity Model using the EDM Wizard. Let’s assume ours is called ShopperBase. Using the data in your project is now very easy.
Access your data through a Data Source
That’s all the code you need to write to populate your DataGrid with the data in the Members table. Very easy isn’t it? But the Members object can do much more than that. It’s a variable of the type ObjectQuery.
It’s very easy to do queries with Entity Framework. There are two primary methods. The first one is using the Entity SQL format which gives you a lot of power but requires a little learning. Luckily it is based on T-SQL so if you are already working with databases the conventional way, learning shouldn’t take long. Here’s a sample of code written this way: -
The example above gets all members who are verified.
But there’s another, and in my opinion better way to get data from your Entity Model. It is by using LINQ to Entities. If you have been coding in .Net 2 and above, you should already be familiar with LINQ. LINQ makes querying available to you inside your programming language. Through LINQ to entities you can query your database using LINQ queries. Here’s the same example re-written to work with LINQ.
See how simple this code is? And you can use the same syntax that you have been writing for your collections and other data inside your program. That’s why I recommend using the LINQ to Entities route to work with data from your Entity Framework.
This concludes our quick but comprehensive tutorial on the Entity Framework. We’ve covered the basics like installation, usage and coding. You should now be ready to work with Entity framework in your own projects. So start using this wonderful new technology today and get Object driven database access within your code.