"Software is like entropy : It Is Difficult to grasp, Weighs nothing, and obeys the Second Law of Thermodynamics, ie, it always increases."
- Norman Augustine I'll
write a series of posts about the Entity Framework, focus on the new version of this tool, Entity Framework 4.0 (EF 4, from now on.) The intention is to address the use of this solution from the beginning and go icrementando some application examples.
In principle I will list some downloads that will allow us to follow the examples and testing.
utlimate Visual Studio 2010 Beta 2
ADO. NET Framework 4.0 Feature Entity Comunity Technology Preview 2
Microsoft SQL Server 2008 Express
Microsoft SQL Server 2008 AdventureWorks Sample Database Product 2008R2 November CTP
Once we have these tools we can put to work and begin testing, for testing with EF 4, I will use the database AdventrueWorks which have left the download link above. Here we can see a screenshot of SQL Management Studio that shows the tables in the database instance.
In this first post I will show how to generate a model from an existing database on my local instance of SQL Server 2008 Express, This first example is very basic and perhaps trivial but it is a first approach to using the Entity Framework and is aimed at those who perhaps did not even know the tool. It is for this reason that this example should not be taken as an example on how to build quality software, it is simply a useful example of knowing when EF 4. Now if we go to Visual Studio 2010 and create a new project (Class Library) that call and appoint DemoEF40.DataL DemoEF40 the solution. As seen in the first picture:
Then we click on the project you just created and the Project menu, select Add Item and select the template there ADO. NET Entity Data Model. After selecting the template is going to open the Entity Data Model Wizard, there select the Generate From Database option and click Next. Once there we will find a wizard that will allow us to connect with our database, select a few tables in the database, those companies will then classes of our model and finally we click the Finish button! This series of steps can be seen in the following Imagna:
In the last picture we see the model generated from the tables selected in the wizard, in the Solution Explorer, see the class generated from T4 code generator, whose name in my case is DemoEF40Model.Designer.cs if right click on this item and its properties can navigate to see the following :
Now we have our model set up and ready for use, by clicking the right mouse button on DemoEF40 solution, then the option Add new project, add a console project to the solution which will call DemoEF40 . Console, with this project we will conduct tests to our data model and verify if it hits correctly in our database. The first thing that will prove to bring data from the database. To do this we will take into account:
- Let's add a reference in our project DemoEF40.DataL DemoEF40.Console the project in the Project menu by clicking on Add Reference.
- setear Let DemoEF40.Console project as Start Up Project.
- Let's add a reference to System.Data.Entity
- Let's copy the configuration file of the project App.conf DemoEF40.Console DemoEF40.DataL the project.
Now let's try a simple code to retrieve some items from the Products table, and the list sigiuente
Class Program {
static void Main (string [] args) {
AdventureWorksEntities AdventureWorksEntities ctx = new ();
foreach (Product b in ctx.Products)
{System.Console.WriteLine (b.Name)
}}}
In this code we can see how to list the products available in our company deals database, corresponding to the records in the Products table. Simple no? We highlight this code to next line:
AdventureWorksEntities AdventureWorksEntities ctx = new ();
In this line we see how we access context that surrounds our model by creating an instance of AdventureWorksEntities, looking at this class in the definition of the model is that we will see that it inherits from ObjectContex, for the moment we go into details on this topic in particular but in successive post we shall see in detail the kind ObjectContext.
Let's see how we can make very Simble consultations with Linq, as we do in the following list:
class Program {
static void Main (string [] args) {
AdventureWorksEntities
ctx = new AdventureWorksEntities ();
var s = from p in ctx.Products
WHERE p.ProductID \u0026lt;100
select p;
foreach (Product b in s) {
System.Console.WriteLine (b.Name +"["+ b.ProductID.ToString ()+"]" ;)}
System.Console.ReadLine ();
}}
Conclusion I've covered this post a first approach to the Entity Framework 4.0 for those who had never used this tool or some other ORM. We saw how to create a model through the Entity Model Wizard, from an existing database (a very common case) and how to bring data from the database to the conceptual model or objects raised and generated automatically. This example is only useful to know the effects of EF, since it has no value in the real world, we shall see in subsequent post as we approach a real world example of the new features of EF 4.
PS: This is the last post of 2009!!, I hope in the coming 2010 we continue sharing our passion for technology and fulfilling our deepest desires and coveted. Starting the year well. And Happy programming!
0 comments:
Post a Comment