Sunday, October 17, 2010

ADO.NET Entity Framework

Welcome every one, I am starting a series of posts on Entity Framework, which covers the details of the framework.

Developers spend too much time in database, its tables and their relationships, stored procedures and views, as well as the schema of the data that they return. In order to make developer’s life more easy Microsoft have introduced ADO.NET Entity Framework, and by using that developers have not to worry about the database access or implementation code.

The ADO.NET Entity Framework is a new data access platform from Microsoft for writing .NET applications. It was released in July 2008 as part of the Visual Studio Service Pack 1 and .NET 3.5 Service Pack 1 and is a new part of the ADO.NET family of technologies. ADO.NET will LINQ-enable many data access components: LINQ to SQL, LINQ to DataSet and LINQ to Entities.

Few years back when there was no built in ORM tools available from Microsoft, developers have choice of nHibernet and other similar kind of frameworks. Later on Microsoft have introduced TypedDatasets, which have some of the ORM features.Basically ORM is an Object-Relational Mapping tool which fills the gap between the database layer and the actual business layer.

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored. With the Entity Framework , developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications. In other words it hides the logical database schema behind the scene and presents the conceptual  schema to the developer. The main goal of this just focus on your business requirements, do not worry about the database plumbing work, like write functions to perform CRUD operations on database. We can also call it to write code for Model not for Database

Before we begin I would like to show you guys a small demo about EF, so you can know what is EF is about and how it is useful. Lets start with a small demo using Northwind database.

  1. Open the Visual Studio and create new console application.
    image

  2. Right click on the Project file, click on Add new item called ADO.NET Entity Data Model. Give a proper name to the model
    image

  3. Entity Data Model Wizard will popup, select option “Generate from existing database”, then press next.

  4. It asks for database connection, provide proper database connection and press Next

  5. After that wizard asks for which database objects you want to include in the project, I am going to select only 2 tables called Category and Products. Once done, then press Finish
    image

  6. Once you are done, you can see the Entity Framework designer on the screen with 2 entities called Products and Categories.
    image

  7. Write down the below code in the main method [sourcecode language="csharp" firstline="1" highlight="6,11,18" padlinenumbers="true" collapse="true" gutter="true" htmlscript="false" light="false" toolbar="true" wraplines="true"]
    static void Main(string[] args)
    {
    using (NorthwindEntities db = new NorthwindEntities())
    {
    //Display all products with their category name
    var products = from p in db.Products select p;
    DisplayProducts("All Products", products);
    //Select products which have price more then 100
    products = from p in db.Products
    where p.UnitPrice > 100 select p;
    DisplayProducts("Costly Products", products);
    //Select all the products of "Beverages" category
    products = from p in db.Products
    where p.Category.CategoryName == "Beverages"
    select p;
    DisplayProducts("List of Beverages", products);
    Console.ReadKey();
    }
    }
    private static void DisplayProducts(string msg, IQueryable<Product> products)
    {
    Console.WriteLine("\n\n" + msg);
    Console.WriteLine("--------------------");
    foreach (var product in products)
    Console.WriteLine(product.ProductName);
    }[/sourcecode]

  8. And what you get is the below output
    image

  9. So what you can see from the code is that you do not have to write any database code. No need to worry about writing SQL queries, or create database connection, execute the statements etc etc. Just write concentrate on your business needs.


So now you have a idea of what entity framework is about. Lets go for a deep dive inside Entity Framework and understand it in more deep.

Here is the Entity Framework path which we are going to follow in my upcoming posts:

  1. Working of Entity Framework

  2. EDM (Entity Data Model)

  3. Creating first EDM and Concept of EDMX file

  4. Programming for EDM

  5. More about Entity programming

  6. POCO

  7. Using ObjectServices

  8. Insided of Metadataworkspace

  9. Customizing Entities and Entity Data Model

No comments:

Post a Comment