Time to look in to ADO.NET Entity Framework.We will try to see what is it and how we can go ahead and implement with code samples.
Why Entity Framework ?
Database development with the .NET framework has not changed a lot since its first release. Many of us usually start by designing our database tables and their relationships and then creating classes in our application then we operate this entities with the help of ADO.NET.This is going to be lengthy process and maintenance is not so easy. Here comes up ADO.NET Entity Framework; it allows you to deal with the entities represented in the database in your application code by abstracting the groundwork and maintenance code work away from you. A very light description of the ADO.NET Entity Framework would be "It allows you to deal with database concepts in your code.".So now we might be thinking the .net frame work and where the Entity Framework is going to fit, on what release,etc let me post an image , to make it lock.
ADO.NET Entity Framework is an object-relational mapping (ORM) framework for the .NET Framework. It is designed to enable developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. The goal is to decrease the amount of code and maintenance required for data-oriented applications.
Lets see the drill down:
Requirements:
· SP1 for .NET Framework 3.5/Visual Studio 2008 (which you can download here.)
· MySQL server 5.0 or higher. Note that Entity Framework support is not available in Standard Edition of dot Connect for MySQL.
But l am having having Microsoft Visual Studio 2010, .NET Framework 4. Installed that is well and good.
Setting Up the Database:
Before we start coding, we will need to create the database and its objects that will be used and referred to in this. We are referring to the same DB that we used for LINQ to SQL.Nothing but TestDB.
Generating Model from Database.
Create a new console application in Visual Studio. It could be any other project type as well, but for simplicity's sake we'll use console project throughout the tutorial.
1. In the Solution Explorer right-click on the project and choose Add | New Item.
2. In the dialog choose ADO.NET Entity Data Model, click Add. This launches Entity Data Model Wizard.
3. In the wizard choose Generate from database, click Next.
4. Pick an existing dotConnect for MySQL connection string or create a new one. When creating a new connection choose MySQL Server in the Data Source list, then dotConnect for MySQL in the Data provider combobox. This step is illustrated in the screenshot below.
6. In the field Save entity connection settings... type TestDemo. This will be the name of the main data access class. Click Next.
7. Choose database objects that will be used in the model. These are all objects from the Test_db script, including auxiliary tables.
8. Press Finish. The model will be generated and opened in EDM Designer.
Now Querying Data:
The model we just generated is ready to use. Name it as Model1.edmx. You can inspect it visually in the designer or take a look behind the scenes with XML Editor.You can see the wizard creates classes for all selected tables that represent entities. It also creates a descendant of System.Data.Objects.ObjectContext class, which controls the connection to the database, and the whole data flow. This class includes properties and methods named after your database objects.All Entity Framework operations are executed through the ObjectContext descendant. To retrieve data you have to first create an instance of the context, then prepare a query with LINQ to Entities or EntitySQL or their mix, and then access the object returned by the query, which may be a collection of objects or a single object.So lets start witting quires.We are posting the code samples for CURD operations.
Now Querying Data:
The model we just generated is ready to use. Name it as Model1.edmx. You can inspect it visually in the designer or take a look behind the scenes with XML Editor.You can see the wizard creates classes for all selected tables that represent entities. It also creates a descendant of System.Data.Objects.ObjectContext class, which controls the connection to the database, and the whole data flow. This class includes properties and methods named after your database objects.All Entity Framework operations are executed through the ObjectContext descendant. To retrieve data you have to first create an instance of the context, then prepare a query with LINQ to Entities or EntitySQL or their mix, and then access the object returned by the query, which may be a collection of objects or a single object.So lets start witting quires.We are posting the code samples for CURD operations.
No comments:
Post a Comment