Thursday, December 29, 2011

Start using .Net Entity Framework - 1

This article illustrates the creation of an Entity Framework model and its use with a small program in C # for Windows (Windows Application).
To implement this example you must have installed Visual Studio 2010 (with 2008 should also work), as well as .Net framework 3.5 SP1 or higher.
In addition, it is necessary to access a database
service, so as the first approach, it is advisable to use Microsoft SQL Server 2008.

1. The first step is to create a new project of type
Windows Forms Application.

2. Then we add a new item to the project, of type ADO.Net Entity Data Model (see next two images). In the example it has been given the name ModeloDeDados.edmx to the file that will contain the data model and the database mapping.







3. Then, to make things much easier, we generate the data model from an existing database. To do this, select "Generate from database" (see the figure below).




This option requires us to set up the database connection, from which the data model and mappings will be generated:
    - Select the server where the
database service is running, the database name, and test the connection:



   - choose to save the connection string in App.Config:


4. Then, choose the database objects that will be part of the model and will be mapped between the model and the database (in this case we have only one table, Paises (meaning countries)):



Finally, we get a graphical view of the model generated from the database.

Next article will illustrate the use of the generated data model from a small C# program.




If anyone wants the code used in the example please ask for it as a comment to this article.

Other related articles:
    - ADO.Net Entity Framework
    - Start using .Net Entity Framework - 2




No comments:

Post a Comment