Yes!!! Finally I have time to look on this, "the Microsoft's solution for database oriented application". I strongly believe this is a cool stuff for LOB developers. No more NHibernate/IBatis/EntLib OR/M, no more SubSonic active record, cos as we expected, Microsoft shipped it (now in Beta 2)!!.
What you will need to follow me in this post are:
- VS 2008 Beta 2 Installed on your machine (Get from PCMedia Magazine)
-
ADO.NET Entity Framework installed
-
ADO.NET Entity Framework Tools installed (with samples and docs)
If I said framework, you can imagine it as a collection of OO classes packaged for specific purpose. ADO.NET entity framework is also like that. It was implemented as two primary managed DLLs at the following location:
%Program Files%\Reference Assemblies\Microsoft\Framework\3.5
- System.Data.Entity.dll
- System.Data.EntityDesign.dll
Those two DLLs are members for .NET framework 3.5 and also we have their copies at GAC folder. ADO.NET Entity Tools gives us EDM Tools at :
%windir%\Microsoft.NET\Framework\v3.5\EdmGen.exe
%windir%\Microsoft.NET\Framework\v3.5\EdmxDeploy.exe
Why Do We Need More Framework?
Every LOB has, explicitly or implicitly, a conceptual data model that describes the various elements of the problem domain, as well as each element's structure, the relationships between each element, their constraints, and so on. We usually think and work with "objects in mind" at the beginning. But then, since currently most applications are written on top of relational databases, sooner or later our objects will have to deal with the data represented in a relational form. This is an impedance mismatch (the difficulty of one system to handle inputs from another system) between OO concept and relational storage.
Basically we can do LOB with "clean ADO.NET". I am totally agreed with that. But when we take a look at the amount of codes that average LOB developers must write to address the impedance mismatch across various data representations (for example objects and relational stores), it is clear that we need more standardize framework. Indeed, there are many enterprise scenarios where the right data access framework can empower developers to focus on the needs of the application, as opposed to the complexities of bridging disparate data representations.
What is ADO.NET Entity Framework?
ADO.NET Entity Framework enables us to work with data in the form of domain-specific objects and properties, just say entities, without having to concern themselves with the underlying database tables and columns where this data is stored. It provides higher level of abstraction called Entity Data Model (EDM), and let me start with it.
EDM is an Entity-Relationship (ER) data model. This ER model is familiar to most if not all LOB developers, and has been used successfully for about 20-30 years. The key concepts introduced by the EDM is metadata. EDM metadata (*.edmx) defines data at both the relational (at storage) and conceptual level and a mapping between the two. It also enables us to program directly against the data types defined at the conceptual level as CLR objects.
A longstanding and common design pattern for data modeling is the division of the data model into three parts: a conceptual model (ex : domain abstraction), a logical model (ex : ER diagram), and a physical model (ex : storage engine features). With Entity Framework you can write code that operates on conceptual entities and relationships, relying on the Entity Framework to map those operations to storage-specific relational commands. No more hard-coded dependencies on a particular RDMS engine or even a particular logical model. With Entity Framework you will have consistent conceptual model across multiple storage engines. What we need to do is to create a conceptual entity data model, a storage entity model, and a mapping between the two. Those are what I called EDMX metadata which encapsulates the form of three types of XML files that have corresponding file name extensions:
- Conceptual Schema Definition Language (.csdl)
- Store Schema Definition Language (.ssdl)
- Mapping specification language (.msl)
Using EDM metadata, the Entity Framework generates a set of classes that programmers use to interact directly with the conceptual model and indirectly with the storage model and the underlying data store. These classes are partial classes that can be extended with additional members added by the developer. Cool !!!
How to Start ?
For INDC Geeks, just install VS2008, EDM Framework and Tools !!. The EDM Wizard starts after you add an ADO.NET Entity Data Model to any .NET 3.5 project in VS 2008. Follow the wizard for your 1st time use to generate an Entity Data Model (EDM) for database objects (Tables, Views, Stored Procedures, Functions). The wizard creates an .edmx which enables us to view and edit the mappings graphically then it creates a source code file that contains the classes generated from the CSDL information encapsulated in the .edmx file. The source code file (C#, VB.NET) is auto-generated and is updated when the .edmx file changes.
Above picture is EDM for AdventureWorks sample database in SQL 2005. I got that by following these steps:
- Create Console Project in C#
- Add Reference to System.Data.Entity
- Add using System.Data.Objects in your code
- In solution explorer, add new item, ADO.NET Entity Data Model
- Follow the Wizard to generate EDM from AdventureWorks database
- In Database Object Form, choose Department and Employee tables
- Use LINQ to do query on EDM object
- Use VSTS 2008 Profiler to see module dependencies
using System;
using System.Linq;
using System.Data.Objects;
using AdventureWorksModel;
namespace EntityFramework
{
class Program {
static void Main(string[] args) {
using (AdventureWorksEntitiesModel db = new AdventureWorksEntitiesModel ())
{
var departments = from d in db.Department
where d.GroupName == "Research and Development"
orderby d.DepartmentID
select d;
foreach (AdventureWorksModel.Department x in departments) {
Console.WriteLine(x.Name);
}
}
Console.ReadLine();
}
}
}
You can review how it works by doing profiling (instrumentation) for the resulted executable like I showed in the following picture.
Who is the man behind the scene ??? As you may guess, EDMGen.EXE !!! For you who are command line fanatics (anti-wizard), don't hesitate to use your fave XML editor and play with it:). For example:
To generate a full Entity Model from the INDCDB sample database.
EdmGen /mode:FullGeneration /project:INDC /provider:System.Data.SqlClient
/connectionstring:"server=RISMAN-VISTA;uid=sa;pwd=xxxxx123;database=INDCDB"
To generate an Entity Model starting from an SSDL file.
EdmGen /mode:FromSSDLGeneration /inssdl:INDCDB.ssdl /project:INDC
To validate an Entity Model.
EdmGen /mode:ValidateArtifacts /inssdl:INDC.ssdl /inmsl:INDC.msl /incsdl:INDC.csdl
Please explore the result of EdmGen /? command line to foster your understanding about what behind the scene. I strongly recommend you to start with command line code generation instead of EDM Wizard. Let me show you in my next Entity Framework posting.
Happy coding !!
Thx - RAM